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 51d8ebd
Showing 1 changed file with 64 additions and 41 deletions.
105 changes: 64 additions & 41 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,68 @@ 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 wg sync.WaitGroup

Check failure on line 190 in network/server_test.go

View workflow job for this annotation

GitHub Actions / Test GatewayD

variable name 'wg' is too short for the scope of its usage (varnamelen)

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")
wg.Add(1)
go func(t *testing.T, server *Server, stop chan struct{}, wg *sync.WaitGroup) {

Check failure on line 193 in network/server_test.go

View workflow job for this annotation

GitHub Actions / Test GatewayD

test helper function should start from t.Helper() (thelper)
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()
wg.Done()
return
default:

Check failure on line 227 in network/server_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)
}
}
}(t, server, stop, &wg)

assert.NoError(t, os.Remove("server_test.log"))
wg.Add(1)
go func(t *testing.T, server *Server, errs chan error, stop chan struct{}, wg *sync.WaitGroup) {

Check failure on line 233 in network/server_test.go

View workflow job for this annotation

GitHub Actions / Test GatewayD

`TestRunServer$6` - `stop` is unused (unparam)
if err := server.Run(); err != nil {
errs <- err
t.Fail()
}
}(server, errs)
wg.Done()
}(t, server, errs, stop, &wg)

wg.Add(1)
//nolint:thelper
go func(t *testing.T, server *Server, proxy *Proxy) {
go func(t *testing.T, server *Server, proxy *Proxy, stop chan struct{}, wg *sync.WaitGroup) {

Check failure on line 243 in network/server_test.go

View workflow job for this annotation

GitHub Actions / Test GatewayD

parameter name 'wg' is too short for the scope of its usage (varnamelen)
// Pause for a while to allow the server to start.
time.Sleep(500 * time.Millisecond)

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 +287,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{}{}
wg.Done()
return
}
time.Sleep(100 * time.Millisecond)
}
}(t, server, proxy)
}(t, server, proxy, stop, &wg)

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

0 comments on commit 51d8ebd

Please sign in to comment.