diff --git a/acceptanceTests/tap_test.go b/acceptanceTests/tap_test.go index af17dd9b6..60c2acb9c 100644 --- a/acceptanceTests/tap_test.go +++ b/acceptanceTests/tap_test.go @@ -66,7 +66,8 @@ func TestTap(t *testing.T) { entriesCheckFunc := func() error { timestamp := time.Now().UnixNano() / int64(time.Millisecond) - entries := checkDBHasEntries(t, timestamp, entriesCount) + entries := getDBEntries(t, timestamp, entriesCount, 1*time.Second) + assertEntriesAtLeast(t, entries, 1) entry := entries[0] entryUrl := fmt.Sprintf("%v/entries/%v", apiServerUrl, entry["id"]) @@ -431,7 +432,8 @@ func TestTapRedact(t *testing.T) { redactCheckFunc := func() error { timestamp := time.Now().UnixNano() / int64(time.Millisecond) - entries := checkDBHasEntries(t, timestamp, defaultEntriesCount) + entries := getDBEntries(t, timestamp, defaultEntriesCount, 1*time.Second) + assertEntriesAtLeast(t, entries, 1) firstEntry := entries[0] entryUrl := fmt.Sprintf("%v/entries/%v", apiServerUrl, firstEntry["id"]) @@ -527,7 +529,8 @@ func TestTapNoRedact(t *testing.T) { redactCheckFunc := func() error { timestamp := time.Now().UnixNano() / int64(time.Millisecond) - entries := checkDBHasEntries(t, timestamp, defaultEntriesCount) + entries := getDBEntries(t, timestamp, defaultEntriesCount, 1*time.Second) + assertEntriesAtLeast(t, entries, 1) firstEntry := entries[0] entryUrl := fmt.Sprintf("%v/entries/%v", apiServerUrl, firstEntry["id"]) @@ -623,7 +626,8 @@ func TestTapRegexMasking(t *testing.T) { redactCheckFunc := func() error { timestamp := time.Now().UnixNano() / int64(time.Millisecond) - entries := checkDBHasEntries(t, timestamp, defaultEntriesCount) + entries := getDBEntries(t, timestamp, defaultEntriesCount, 1*time.Second) + assertEntriesAtLeast(t, entries, 1) firstEntry := entries[0] entryUrl := fmt.Sprintf("%v/entries/%v", apiServerUrl, firstEntry["id"]) @@ -711,7 +715,8 @@ func TestTapIgnoredUserAgents(t *testing.T) { ignoredUserAgentsCheckFunc := func() error { timestamp := time.Now().UnixNano() / int64(time.Millisecond) - entries := checkDBHasEntries(t, timestamp, defaultEntriesCount) + entries := getDBEntries(t, timestamp, defaultEntriesCount, 1*time.Second) + assertEntriesAtLeast(t, entries, 1) for _, entryInterface := range entries { entryUrl := fmt.Sprintf("%v/entries/%v", apiServerUrl, entryInterface["id"]) @@ -901,7 +906,8 @@ func TestDaemonSeeTraffic(t *testing.T) { entriesCheckFunc := func() error { timestamp := time.Now().UnixNano() / int64(time.Millisecond) - entries := checkDBHasEntries(t, timestamp, entriesCount) + entries := getDBEntries(t, timestamp, entriesCount, 1*time.Second) + assertEntriesAtLeast(t, entries, 1) entry := entries[0] entryUrl := fmt.Sprintf("%v/entries/%v", apiServerUrl, entry["id"]) diff --git a/acceptanceTests/testsUtils.go b/acceptanceTests/testsUtils.go index 0952e6734..b9f43d4e8 100644 --- a/acceptanceTests/testsUtils.go +++ b/acceptanceTests/testsUtils.go @@ -334,9 +334,17 @@ func waitTimeout(wg *sync.WaitGroup, timeout time.Duration) bool { } } -// checkDBHasEntries checks whether there are any entries in the database -// before the given timestamp. Returns a slice of non-empty entries if it succeeds. -func checkDBHasEntries(t *testing.T, timestamp int64, limit int) (entries []map[string]interface{}) { +// assertEntriesAtLeast checks whether the number of entries greater than or equal to n +func assertEntriesAtLeast(t *testing.T, entries []map[string]interface{}, n int) { + if len(entries) < n { + t.Errorf("Unexpected entries result - Expected more than %d entries", n-1) + } +} + +// getDBEntries retrieves the entries from the database before the given timestamp. +// Also limits the results according to the limit parameter. +// Timeout for the WebSocket connection is defined by the timeout parameter. +func getDBEntries(t *testing.T, timestamp int64, limit int, timeout time.Duration) (entries []map[string]interface{}) { query := fmt.Sprintf("timestamp < %d and limit(%d)", timestamp, limit) webSocketUrl := getWebSocketUrl(defaultApiServerPort) @@ -377,11 +385,7 @@ func checkDBHasEntries(t *testing.T, timestamp int64, limit int) (entries []map[ go handleWSConnection(&wg) wg.Add(1) - waitTimeout(&wg, 1*time.Second) - - if len(entries) == 0 { - t.Error("unexpected entries result - Expected more than 0 entries") - } + waitTimeout(&wg, timeout) return }