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 f377c83
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
2 changes: 1 addition & 1 deletion network/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (engine *Engine) Stop(ctx context.Context) error {
}
engine.stopServer <- struct{}{}
close(engine.stopServer)
return err
return err //nolint:wrapcheck
}

// NewEngine creates a new engine.
Expand Down
51 changes: 51 additions & 0 deletions network/engine_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
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) {
v := <-engine.stopServer
// Empty struct is expected to be received and
// it means that the server is stopped.
assert.Equal(t, v, struct{}{})
}(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 f377c83

Please sign in to comment.