Skip to content

Commit

Permalink
Fix: deterministic check for FlowDeleted (#397)
Browse files Browse the repository at this point in the history
* Explicit server close, remove delete check
* Use channel to signal complete shutdown
* Longer timeout
  • Loading branch information
stephen-soltesz authored Nov 7, 2023
1 parent ea3439c commit 18e4fa9
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions ndt7/handler/handler_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
// fakeServer implements the eventsocket.Server interface for testing the ndt7 handler.
type fakeServer struct {
created int
deleted int
deleted chan bool
}

func (f *fakeServer) Listen() error { return nil }
Expand All @@ -27,14 +27,13 @@ func (f *fakeServer) FlowCreated(timestamp time.Time, uuid string, sockid inetdi
f.created++
}
func (f *fakeServer) FlowDeleted(timestamp time.Time, uuid string) {
f.deleted++
close(f.deleted)
}

func TestHandler_Download(t *testing.T) {
t.Run("download flow events", func(t *testing.T) {
fs := &fakeServer{}
fs := &fakeServer{deleted: make(chan bool)}
ndt7h, srv := ndt7test.NewNDT7Server(t)
defer srv.Close()
// Override the handler Events server with our fake server.
ndt7h.Events = fs

Expand All @@ -45,15 +44,22 @@ func TestHandler_Download(t *testing.T) {
if err != nil && !websocket.IsCloseError(err, websocket.CloseNormalClosure) {
testingx.Must(t, err, "failed to download")
}
// Wait briefly for connection close event.
time.Sleep(time.Second)
srv.Close()

// Verify that both events have occurred once.
if fs.created == 0 {
t.Errorf("flow events created not detected; got %d, want 1", fs.created)
}
if fs.deleted == 0 {
t.Errorf("flow events deleted not detected; got %d, want 1", fs.deleted)
// Since the connection handler goroutine shutdown is independent of the
// server and client connection shutdowns, wait for the fakeServer to
// receive the delete flow message up to 15 seconds.
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
select {
case <-ctx.Done():
t.Errorf("flow events not deleted before timeout")
case <-fs.deleted:
// Success.
}
})
}
Expand Down

0 comments on commit 18e4fa9

Please sign in to comment.