Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#69: Added more tests for SET command #399

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions tests/set_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package tests

import (
"fmt"
"net"
"strconv"
"sync"
"testing"
"time"

"github.com/dicedb/dice/testutils"
"gotest.tools/v3/assert"
)

Expand All @@ -16,6 +20,9 @@ type TestCase struct {

func TestSet(t *testing.T) {
conn := getLocalConnection()
keyValLen := 200
longKey := testutils.GenerateRandomString(keyValLen, "abc123@#$")
longVal := testutils.GenerateRandomString(keyValLen, "abc123@#$")
defer conn.Close()

testCases := []TestCase{
Expand All @@ -34,6 +41,16 @@ func TestSet(t *testing.T) {
commands: []string{"SET k v1", "SET k 5", "GET k"},
expected: []interface{}{"OK", "OK", int64(5)},
},
{
name: "Set and get a long key",
commands: []string{"SET " + *longKey + " " + *longVal, "GET " + *longKey},
expected: []interface{}{"OK", *longVal},
},
{
name: "Set and get a boolean",
commands: []string{"SET k true", "GET k"},
expected: []interface{}{"OK", "true"},
},
}

for _, tc := range testCases {
Expand Down Expand Up @@ -198,3 +215,56 @@ func TestWithKeepTTLFlag(t *testing.T) {

assert.Equal(t, out, fireCommand(conn, cmd), "Value mismatch for cmd %s\n.", cmd)
}

/*
We open some connections to the db and fire concurrent SET commands for a particular key
with different values. We expect that there are no dirty reads/writes.
All the values read should be among the values that were attempted to set in the first place.
*/
func TestConcurrentSetCommands(t *testing.T) {
numOfConnections := 4
connectionValues := make(map[*net.Conn]*string)
expectedValues := make(map[string]struct{})
valuesReadChan := make(chan interface{}, numOfConnections)

// Create connections and the values to set through them.
for connNum := 0; connNum < numOfConnections; connNum++ {
value := testutils.GenerateRandomString(8, "abcdefghizklmopqrs12345")
JyotinderSingh marked this conversation as resolved.
Show resolved Hide resolved
connectionValues[getLocalConnectionPtr()] = value
expectedValues[*value] = struct{}{}
}

// Execute the SET commands from the connections, and pass the output of GET to a channel
var wgroup sync.WaitGroup
key := "sample_key"
for conn, value := range connectionValues {
wgroup.Add(1)
go executeCommands(conn, &key, value, valuesReadChan, &wgroup)
}
wgroup.Wait()
fmt.Println("Received values from all connections")
close(valuesReadChan)

// Verify the values received in the channel
assert.Equal(t, numOfConnections, len(valuesReadChan))
for valueRead := range valuesReadChan {
if valueRead != nil {
valueReadStr := valueRead.(string)
_, ok := expectedValues[valueReadStr]
if !ok {
fmt.Println("Value read is not in expected values' map")
t.Fail()
break
}
}
}
}

func executeCommands(conn *net.Conn, key, value *string, valReadChan chan interface{}, wGroup *sync.WaitGroup) {
defer wGroup.Done()
defer (*conn).Close()
fireCommand(*conn, "SET "+*key+" "+*value)
var readValue = fireCommand(*conn, "GET "+*key)
valReadChan <- readValue
fmt.Println("Goroutine done")
}
5 changes: 5 additions & 0 deletions tests/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
return conn
}

func getLocalConnectionPtr() *net.Conn {

Check failure on line 28 in tests/setup.go

View workflow job for this annotation

GitHub Actions / lint

func `getLocalConnectionPtr` is unused (unused)

Check failure on line 28 in tests/setup.go

View workflow job for this annotation

GitHub Actions / lint

ptrToRefParam: consider to make non-pointer type for `*net.Conn` (gocritic)
conn := getLocalConnection()
return &conn
}
JyotinderSingh marked this conversation as resolved.
Show resolved Hide resolved

// deleteTestKeys is a utility to delete a list of keys before running a test
//
//nolint:unused
Expand Down
17 changes: 17 additions & 0 deletions testutils/random_nums.go
JyotinderSingh marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package testutils

import (
"math/rand"
"time"
)

var randIndex = rand.New(rand.NewSource(time.Now().UnixNano()))

Check failure on line 8 in testutils/random_nums.go

View workflow job for this annotation

GitHub Actions / lint

G404: Use of weak random number generator (math/rand or math/rand/v2 instead of crypto/rand) (gosec)

func GenerateRandomString(length int, charset string) *string {
outputBytes := make([]byte, length)
for i := range outputBytes {
outputBytes[i] = charset[randIndex.Intn(len(charset))]
}
outputStr := string(outputBytes)
return &outputStr
}
Loading