Skip to content

Commit

Permalink
feat: add randomness to concurrent txn test
Browse files Browse the repository at this point in the history
- In test_txn_discard, changed the number of uniqueKeys generated from 3 to 2.
- In test_txn_multi_tx, updated the commands to set key2 with a random integer value and increment key1.
- Also added a comment explaining the expected result in each transaction.
- In test_txn_queue, updated the command to set a random key with a random integer value and increment it.
  • Loading branch information
ryan-gang committed Jun 14, 2024
1 parent 63b9b01 commit 263a9cb
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
2 changes: 1 addition & 1 deletion internal/test_txn_discard.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func testTxDiscard(stageHarness *test_case_harness.TestCaseHarness) error {
}
defer client.Close()

uniqueKeys := random.RandomWords(3)
uniqueKeys := random.RandomWords(2)
key1, key2 := uniqueKeys[0], uniqueKeys[1]
randomInt1, randomInt2 := random.RandomInt(1, 100), random.RandomInt(1, 100)

Expand Down
29 changes: 18 additions & 11 deletions internal/test_txn_multi_tx.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package internal

import (
"fmt"

"github.com/codecrafters-io/redis-tester/internal/redis_executable"
resp_value "github.com/codecrafters-io/redis-tester/internal/resp/value"
"github.com/codecrafters-io/redis-tester/internal/resp_assertions"

"github.com/codecrafters-io/redis-tester/internal/test_cases"
"github.com/codecrafters-io/tester-utils/random"
"github.com/codecrafters-io/tester-utils/test_case_harness"
)

Expand All @@ -25,11 +28,15 @@ func testTxMultiTx(stageHarness *test_case_harness.TestCaseHarness) error {
defer client.Close()
}

uniqueKeys := random.RandomWords(2)
key1, key2 := uniqueKeys[0], uniqueKeys[1]
randomIntegerValue := random.RandomInt(1, 100)

for i, client := range clients {
multiCommandTestCase := test_cases.MultiCommandTestCase{
Commands: [][]string{
{"SET", "bar", "7"},
{"INCR", "foo"},
{"SET", key2, fmt.Sprint(randomIntegerValue)},
{"INCR", key1},
},
Assertions: []resp_assertions.RESPAssertion{
resp_assertions.NewStringAssertion("OK"),
Expand All @@ -42,13 +49,12 @@ func testTxMultiTx(stageHarness *test_case_harness.TestCaseHarness) error {
}
}

for i, client := range clients {
for _, client := range clients {
transactionTestCase := test_cases.TransactionTestCase{
CommandQueue: [][]string{
{"INCR", "foo"},
{"INCR", "bar"},
{"INCR", key1},
{"INCR", key2},
},
ResultArray: []resp_value.Value{resp_value.NewIntegerValue(4 + i), resp_value.NewIntegerValue(8 + i)},
}
if err := transactionTestCase.RunWithoutExec(client, logger); err != nil {
return err
Expand All @@ -57,11 +63,12 @@ func testTxMultiTx(stageHarness *test_case_harness.TestCaseHarness) error {

for i, client := range clients {
transactionTestCase := test_cases.TransactionTestCase{
CommandQueue: [][]string{
{"INCR", "foo"},
{"INCR", "bar"},
},
ResultArray: []resp_value.Value{resp_value.NewIntegerValue(4 + i), resp_value.NewIntegerValue(8 + i)},
// Before a single transaction is queued,
// We run 3x INCR key1, and set key2 to randomIntegerValue
// Inside each transaction, we run 1x INCR key1, key2
// So it increases by 1 for each transaction
// `i` here is 0-indexed, so we add 1 to the expected value
ResultArray: []resp_value.Value{resp_value.NewIntegerValue(3 + (1 + i)), resp_value.NewIntegerValue(randomIntegerValue + (1 + i))},
}
if err := transactionTestCase.RunExec(client, logger); err != nil {
return err
Expand Down
12 changes: 9 additions & 3 deletions internal/test_txn_queue.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package internal

import (
"fmt"

"github.com/codecrafters-io/redis-tester/internal/redis_executable"
resp_value "github.com/codecrafters-io/redis-tester/internal/resp/value"
"github.com/codecrafters-io/redis-tester/internal/resp_assertions"

"github.com/codecrafters-io/redis-tester/internal/test_cases"
"github.com/codecrafters-io/tester-utils/random"
"github.com/codecrafters-io/tester-utils/test_case_harness"
)

Expand All @@ -25,10 +28,13 @@ func testTxQueue(stageHarness *test_case_harness.TestCaseHarness) error {
defer client.Close()
}

key := random.RandomWord()
randomIntegerValue := random.RandomInt(1, 100)

transactionTestCase := test_cases.TransactionTestCase{
CommandQueue: [][]string{
{"SET", "foo", "41"},
{"INCR", "foo"},
{"SET", key, fmt.Sprint(randomIntegerValue)},
{"INCR", key},
},
ResultArray: []resp_value.Value{},
}
Expand All @@ -39,7 +45,7 @@ func testTxQueue(stageHarness *test_case_harness.TestCaseHarness) error {

commandTestCase := test_cases.SendCommandTestCase{
Command: "GET",
Args: []string{"foo"},
Args: []string{key},
Assertion: resp_assertions.NewNilAssertion(),
}

Expand Down

0 comments on commit 263a9cb

Please sign in to comment.