Skip to content

Commit

Permalink
Fix server test
Browse files Browse the repository at this point in the history
  • Loading branch information
mostafa committed Oct 9, 2023
1 parent a219b40 commit 74856a0
Showing 1 changed file with 67 additions and 42 deletions.
109 changes: 67 additions & 42 deletions network/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"io"
"os"
"sync"
"testing"
"time"

Expand All @@ -22,6 +23,7 @@ import (
// TestRunServer tests an entire server run with a single client connection and hooks.
func TestRunServer(t *testing.T) {
errs := make(chan error)
defer close(errs)

logger := logging.NewLogger(context.Background(), logging.LoggerConfig{
Output: []config.LogOutput{
Expand All @@ -30,7 +32,7 @@ func TestRunServer(t *testing.T) {
},
TimeFormat: zerolog.TimeFormatUnix,
ConsoleTimeFormat: time.RFC3339,
Level: zerolog.WarnLevel,
Level: zerolog.DebugLevel,
NoColor: true,
FileName: "server_test.log",
})
Expand Down Expand Up @@ -182,43 +184,70 @@ func TestRunServer(t *testing.T) {
)
assert.NotNil(t, server)

go func(server *Server, errs chan error) {
if err := server.Run(); err != nil {
errs <- err
}
close(errs)

// Read the log file and check if the log file contains the expected log messages.
if _, err := os.Stat("server_test.log"); err == nil {
logFile, err := os.Open("server_test.log")
assert.NoError(t, err)
defer logFile.Close()

reader := bufio.NewReader(logFile)
assert.NotNil(t, reader)
stop := make(chan struct{})
defer close(stop)

buffer, err := io.ReadAll(reader)
assert.NoError(t, err)
assert.Greater(t, len(buffer), 0) // The log file should not be empty.
var waitGroup sync.WaitGroup

logLines := string(buffer)
assert.Contains(t, logLines, "GatewayD is running", "GatewayD should be running")
assert.Contains(t, logLines, "GatewayD is ticking...", "GatewayD should be ticking")
assert.Contains(t, logLines, "Ingress traffic", "Ingress traffic should be logged")
assert.Contains(t, logLines, "Egress traffic", "Egress traffic should be logged")
assert.Contains(t, logLines, "GatewayD is shutting down...", "GatewayD should be shutting down")
waitGroup.Add(1)
go func(t *testing.T, server *Server, stop chan struct{}, waitGroup *sync.WaitGroup) {
t.Helper()
for {
select {
case <-stop:
// Read the log file and check if the log file contains the expected log messages.
if _, err := os.Stat("server_test.log"); err == nil {
logFile, err := os.Open("server_test.log")
assert.NoError(t, err)
defer logFile.Close()

reader := bufio.NewReader(logFile)
assert.NotNil(t, reader)

buffer, err := io.ReadAll(reader)
assert.NoError(t, err)
assert.Greater(t, len(buffer), 0) // The log file should not be empty.

logLines := string(buffer)
assert.Contains(t, logLines, "GatewayD is running", "GatewayD should be running")
assert.Contains(t, logLines, "GatewayD is ticking...", "GatewayD should be ticking")
assert.Contains(t, logLines, "Ingress traffic", "Ingress traffic should be logged")
assert.Contains(t, logLines, "Egress traffic", "Egress traffic should be logged")
assert.Contains(t, logLines, "GatewayD is shutting down...", "GatewayD should be shutting down")

assert.NoError(t, os.Remove("server_test.log"))
server.Shutdown()
}
return
case err := <-errs:
server.Shutdown()
t.Log(err)
t.Fail()
waitGroup.Done()
return
default: //nolint:staticcheck
}
}
}(t, server, stop, &waitGroup)

assert.NoError(t, os.Remove("server_test.log"))
waitGroup.Add(1)
go func(t *testing.T, server *Server, errs chan error, waitGroup *sync.WaitGroup) {
t.Helper()
if err := server.Run(); err != nil {
errs <- err
t.Fail()
}
}(server, errs)
waitGroup.Done()
}(t, server, errs, &waitGroup)

waitGroup.Add(1)
go func(t *testing.T, server *Server, proxy *Proxy, stop chan struct{}, waitGroup *sync.WaitGroup) {
t.Helper()
// Pause for a while to allow the server to start.
time.Sleep(500 * time.Millisecond)

//nolint:thelper
go func(t *testing.T, server *Server, proxy *Proxy) {
for {
if server.IsRunning() {
// Pause for a while to allow the server to start.
time.Sleep(500 * time.Millisecond)

client := NewClient(
context.Background(),
&config.Client{
Expand Down Expand Up @@ -260,19 +289,15 @@ func TestRunServer(t *testing.T) {
// Test Prometheus metrics.
CollectAndComparePrometheusMetrics(t)

// Clean up.
client.Close()
// Pause for a while to allow the server to disconnect and shutdown.
time.Sleep(500 * time.Millisecond)
server.Shutdown()
break
time.Sleep(100 * time.Millisecond)
stop <- struct{}{}
waitGroup.Done()
return
}
time.Sleep(100 * time.Millisecond)
}
}(t, server, proxy)
}(t, server, proxy, stop, &waitGroup)

for err := range errs {
if err != nil {
t.Fatal(err)
}
}
waitGroup.Wait()
}

0 comments on commit 74856a0

Please sign in to comment.