From 7b7dc3a4ab467d18cef23a25f5445cf5f5773370 Mon Sep 17 00:00:00 2001 From: Mostafa Moradian Date: Sun, 15 Oct 2023 17:55:07 +0200 Subject: [PATCH] Add test for Engine --- network/engine_test.go | 57 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 network/engine_test.go diff --git a/network/engine_test.go b/network/engine_test.go new file mode 100644 index 00000000..d6e6e6df --- /dev/null +++ b/network/engine_test.go @@ -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: + } + } + }(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") +}