From b1f8fb4b57474855e18e422e9e42242dd401a78b Mon Sep 17 00:00:00 2001 From: Juan Calderon Date: Sat, 10 Dec 2022 22:37:37 -0800 Subject: [PATCH] Use channels to sync ristretto tests --- ristretto/ristretto_test.go | 60 +++++++++++++++++++++++++++---------- 1 file changed, 44 insertions(+), 16 deletions(-) diff --git a/ristretto/ristretto_test.go b/ristretto/ristretto_test.go index 0fbf6b14..f235d2a7 100644 --- a/ristretto/ristretto_test.go +++ b/ristretto/ristretto_test.go @@ -39,15 +39,29 @@ func Test_Ristretto_Get(t *testing.T) { val = []byte("doe") ) - err := testStore.Set(key, val, 0) - utils.AssertEqual(t, nil, err) + // Create a channel to synchronize the test + done := make(chan struct{}) - // stabilize with some delay in between -> bug already communicated - time.Sleep(10000) + // Set the value in a goroutine + go func() { + err := testStore.Set(key, val, 0) + utils.AssertEqual(t, nil, err) - result, err := testStore.Get(key) - utils.AssertEqual(t, nil, err) - utils.AssertEqual(t, val, result) + // Send a value on the channel to signal that the value has been set + done <- struct{}{} + }() + + // Wait for the value to be set or a timeout to occur + select { + case <-done: + // The value has been set, proceed with the test + result, err := testStore.Get(key) + utils.AssertEqual(t, nil, err) + utils.AssertEqual(t, val, result) + case <-time.After(1 * time.Second): + // The value was not set within 1 second, fail the test + t.Errorf("timed out waiting for value to be set") + } } func Test_Ristretto_Set_Expiration(t *testing.T) { @@ -86,18 +100,32 @@ func Test_Ristretto_Delete(t *testing.T) { val = []byte("doe") ) - err := testStore.Set(key, val, 0) - utils.AssertEqual(t, nil, err) + // Create a channel to synchronize the test + done := make(chan struct{}) - // stabilize with some delay in between -> bug already communicated - time.Sleep(10000) + // Set the value in a goroutine + go func() { + err := testStore.Set(key, val, 0) + utils.AssertEqual(t, nil, err) - err = testStore.Delete(key) - utils.AssertEqual(t, nil, err) + // Send a value on the channel to signal that the value has been set + done <- struct{}{} + }() - result, err := testStore.Get(key) - utils.AssertEqual(t, nil, err) - utils.AssertEqual(t, true, len(result) == 0) + // Wait for the value to be set or a timeout to occur + select { + case <-done: + // The value has been set, proceed with the test + err := testStore.Delete(key) + utils.AssertEqual(t, nil, err) + + result, err := testStore.Get(key) + utils.AssertEqual(t, nil, err) + utils.AssertEqual(t, true, len(result) == 0) + case <-time.After(1 * time.Second): + // The value was not set within 1 second, fail the test + t.Errorf("timed out waiting for value to be set") + } } func Test_Ristretto_Reset(t *testing.T) {