Skip to content

Commit

Permalink
chore: add memory and goroutine leak test
Browse files Browse the repository at this point in the history
Signed-off-by: Valery Piashchynski <[email protected]>
  • Loading branch information
rustatian committed Nov 19, 2024
1 parent 1b8af58 commit 9b6e1df
Showing 1 changed file with 35 additions and 5 deletions.
40 changes: 35 additions & 5 deletions tests/kv_memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,13 @@ func TestInMemoryOrder(t *testing.T) {
time.Sleep(time.Second * 1)
stopCh <- struct{}{}
wg.Wait()

t.Cleanup(func() {

})
}

func TestSetManyMemory(t *testing.T) {
t.Skip("This test is executed manually")
cont := endure.New(slog.LevelDebug)

cfg := &config.Plugin{
Expand Down Expand Up @@ -158,6 +161,11 @@ func TestSetManyMemory(t *testing.T) {

time.Sleep(time.Second * 1)

ms := &runtime.MemStats{}
runtime.ReadMemStats(ms)
prevAlloc := ms.Alloc
ngprev := runtime.NumGoroutine()

conn, err := net.Dial("tcp", "127.0.0.1:6666")
assert.NoError(t, err)
client := rpc.NewClientWithCodec(goridgeRpc.NewClientCodec(conn))
Expand Down Expand Up @@ -190,14 +198,33 @@ func TestSetManyMemory(t *testing.T) {
}

ret := &kvProto.Response{}
for i := 0; i < 1000000; i++ {
t.Log("Number Goroutines: ", runtime.NumGoroutine())
for i := 0; i < 10000; i++ {
err = client.Call("kv.Set", data, ret)
require.NoError(t, err)
}
runtime.GC()

ms = &runtime.MemStats{}
runtime.ReadMemStats(ms)
currAlloc := ms.Alloc
currNg := runtime.NumGoroutine()

if currAlloc-prevAlloc > 10000000 { // 10MB
t.Log("Prev alloc", prevAlloc)
t.Log("Curr alloc", currAlloc)
t.Error("Memory leak detected")
}

if currNg-ngprev > 10 {
t.Log("Prev ng", ngprev)
t.Log("Curr ng", currNg)
t.Error("Goroutine leak detected")
}

t.Log("sleeping")
time.Sleep(time.Minute * 1)
time.Sleep(time.Second * 5)

err = client.Call("kv.Clear", data, ret)
require.NoError(t, err)

stopCh <- struct{}{}
wg.Wait()
Expand Down Expand Up @@ -461,4 +488,7 @@ func testRPCMethodsInMemory(t *testing.T) {
err = client.Call("kv.Has", dataClear, ret)
assert.NoError(t, err)
assert.Len(t, ret.GetItems(), 0) // should be 5

err = client.Call("kv.Clear", data, ret)
require.NoError(t, err)
}

0 comments on commit 9b6e1df

Please sign in to comment.