Skip to content

Commit ce3e2fc

Browse files
committed
fix: make TestDropNamespaceErr resilient to async namespace operations
DropAll and DropNamespace are asynchronous operations. The test was flaky because it called ListNamespaces immediately after these operations, before the state had propagated. Added polling helper to wait for the expected namespace count.
1 parent 1d4b617 commit ce3e2fc

1 file changed

Lines changed: 32 additions & 3 deletions

File tree

edgraph/namespace_test.go

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,40 @@ package edgraph
1010
import (
1111
"context"
1212
"testing"
13+
"time"
1314

1415
"github.com/stretchr/testify/require"
1516

1617
"github.com/dgraph-io/dgraph/v25/dgraphapi"
1718
"github.com/dgraph-io/dgraph/v25/dgraphtest"
1819
)
1920

21+
// waitForNamespaceCount polls ListNamespaces until the expected count is reached or timeout.
22+
// This is needed because DropAll and DropNamespace are asynchronous operations.
23+
func waitForNamespaceCount(t *testing.T, client *dgraphapi.GrpcClient, expected int, timeout time.Duration) {
24+
t.Helper()
25+
ctx := context.Background()
26+
deadline := time.Now().Add(timeout)
27+
28+
for time.Now().Before(deadline) {
29+
nsMaps, err := client.ListNamespaces(ctx)
30+
if err != nil {
31+
t.Logf("ListNamespaces error (will retry): %v", err)
32+
time.Sleep(100 * time.Millisecond)
33+
continue
34+
}
35+
if len(nsMaps) == expected {
36+
return
37+
}
38+
time.Sleep(100 * time.Millisecond)
39+
}
40+
41+
// Final check with assertion on timeout
42+
nsMaps, err := client.ListNamespaces(ctx)
43+
require.NoError(t, err)
44+
require.Len(t, nsMaps, expected)
45+
}
46+
2047
func TestNamespaces(t *testing.T) {
2148
dc := dgraphtest.NewComposeCluster()
2249
client, cleanup, err := dc.Client()
@@ -85,6 +112,9 @@ func TestDropNamespaceErr(t *testing.T) {
85112
dgraphapi.DefaultUser, dgraphapi.DefaultPassword))
86113
require.NoError(t, client.DropAll())
87114

115+
// Wait for DropAll to complete (async operation)
116+
waitForNamespaceCount(t, client, 1, 30*time.Second)
117+
88118
// create ns1
89119
ctx := context.Background()
90120
ns1, err := client.CreateNamespace(ctx)
@@ -94,7 +124,6 @@ func TestDropNamespaceErr(t *testing.T) {
94124
require.NoError(t, client.DropNamespace(ctx, ns1))
95125
require.NoError(t, client.DropNamespace(ctx, uint64(10000000)))
96126

97-
nsMaps, err := client.ListNamespaces(ctx)
98-
require.NoError(t, err)
99-
require.Len(t, nsMaps, 1)
127+
// Wait for DropNamespace to complete (async operation)
128+
waitForNamespaceCount(t, client, 1, 30*time.Second)
100129
}

0 commit comments

Comments
 (0)