Skip to content

Commit

Permalink
Add test for Engine
Browse files Browse the repository at this point in the history
  • Loading branch information
mostafa committed Oct 15, 2023
1 parent f1c70bf commit 7b7dc3a
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions network/engine_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package network

import (
"context"
"testing"
"time"

"github.com/gatewayd-io/gatewayd/config"
"github.com/gatewayd-io/gatewayd/logging"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"github.com/zenizh/go-capturer"
)

func TestEngine(t *testing.T) {
output := capturer.CaptureOutput(func() {
logger := logging.NewLogger(context.Background(), logging.LoggerConfig{
Output: []config.LogOutput{config.Console},
TimeFormat: zerolog.TimeFormatUnix,
ConsoleTimeFormat: time.RFC3339,
Level: zerolog.WarnLevel,
NoColor: true,
})
engine := NewEngine(logger)
assert.NotNil(t, engine)
assert.NotNil(t, engine.logger)
assert.Zero(t, engine.connections)
assert.Zero(t, engine.CountConnections())
assert.Empty(t, engine.host)
assert.Empty(t, engine.port)
assert.False(t, engine.running.Load())

go func(engine Engine) {
for {
select {
case v := <-engine.stopServer:
// Empty struct is expected to be received and
// it means that the server is stopped.
assert.Equal(t, v, struct{}{})
break
default:

Check failure on line 41 in network/engine_test.go

View workflow job for this annotation

GitHub Actions / Test GatewayD

SA5004: should not have an empty default case in a for+select loop; the loop will spin (staticcheck)
}
}
}(engine)

err := engine.Stop(context.Background())
// This is expected to fail because the engine is not running.
assert.Nil(t, err)
assert.False(t, engine.running.Load())
assert.Zero(t, engine.connections)
})

t.Log(output)

// Expected output:
assert.Contains(t, output, "ERR Listener is not initialized")
}

0 comments on commit 7b7dc3a

Please sign in to comment.