Skip to content

Commit

Permalink
feat: Add multiple clients to testTxErr, testTxSuccess, and testTxDis…
Browse files Browse the repository at this point in the history
…card

- Added support for multiple clients in the functions `testTxErr`, `testTxSuccess`, and `testTxDiscard`.
- Each client is created with a unique name using the format "client-{index+1}".
- The clients are stored in an array for later use.
- The clients are closed using the `Close` method before returning from the function.
  • Loading branch information
ryan-gang committed Jun 13, 2024
1 parent c1e381f commit a5de8c0
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 17 deletions.
37 changes: 30 additions & 7 deletions internal/test_txn_10.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package internal

import (
"fmt"

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

"github.com/codecrafters-io/redis-tester/internal/instrumented_resp_connection"
Expand All @@ -18,12 +21,17 @@ func testTxErr(stageHarness *test_case_harness.TestCaseHarness) error {

logger := stageHarness.Logger

client, err := instrumented_resp_connection.NewFromAddr(stageHarness, "localhost:6379", "client1")
if err != nil {
logFriendlyError(logger, err)
return err
var clients []*resp_connection.RespConnection

for i := 0; i < 2; i++ {
client, err := instrumented_resp_connection.NewFromAddr(stageHarness, "localhost:6379", fmt.Sprintf("client-%d", i+1))
if err != nil {
logFriendlyError(logger, err)
return err
}
clients = append(clients, client)
defer client.Close()
}
defer client.Close()

multiCommandTestCase := test_cases.MultiCommandTestCase{
Commands: [][]string{
Expand All @@ -36,7 +44,7 @@ func testTxErr(stageHarness *test_case_harness.TestCaseHarness) error {
},
}

if err := multiCommandTestCase.RunAll(client, logger); err != nil {
if err := multiCommandTestCase.RunAll(clients[0], logger); err != nil {
return err
}

Expand All @@ -49,5 +57,20 @@ func testTxErr(stageHarness *test_case_harness.TestCaseHarness) error {
resp_value.NewErrorValue("ERR value is not an integer or out of range"), resp_value.NewIntegerValue(8)},
}

return transactionTestCase.RunAll(client, logger)
if err := transactionTestCase.RunAll(clients[0], logger); err != nil {
return err
}

multiCommandTestCase = test_cases.MultiCommandTestCase{
Commands: [][]string{
{"GET", "bar"},
{"GET", "foo"},
},
Assertions: []resp_assertions.RESPAssertion{
resp_assertions.NewStringAssertion("8"),
resp_assertions.NewStringAssertion("abc"),
},
}

return multiCommandTestCase.RunAll(clients[1], logger)
}
6 changes: 4 additions & 2 deletions internal/test_txn_11.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package internal

import (
"fmt"

"github.com/codecrafters-io/redis-tester/internal/redis_executable"
resp_connection "github.com/codecrafters-io/redis-tester/internal/resp/connection"
resp_value "github.com/codecrafters-io/redis-tester/internal/resp/value"
Expand All @@ -11,7 +13,7 @@ import (
"github.com/codecrafters-io/tester-utils/test_case_harness"
)

func testTxMulti(stageHarness *test_case_harness.TestCaseHarness) error {
func testTxMultiTx(stageHarness *test_case_harness.TestCaseHarness) error {

Check failure on line 16 in internal/test_txn_11.go

View workflow job for this annotation

GitHub Actions / lint

func testTxMultiTx is unused (U1000)
b := redis_executable.NewRedisExecutable(stageHarness)
if err := b.Run(); err != nil {
return err
Expand All @@ -22,7 +24,7 @@ func testTxMulti(stageHarness *test_case_harness.TestCaseHarness) error {
var clients []*resp_connection.RespConnection

for i := 0; i < 3; i++ {
client, err := instrumented_resp_connection.NewFromAddr(stageHarness, "localhost:6379", "client1")
client, err := instrumented_resp_connection.NewFromAddr(stageHarness, "localhost:6379", fmt.Sprintf("client-%d", i+1))
if err != nil {
logFriendlyError(logger, err)
return err
Expand Down
31 changes: 25 additions & 6 deletions internal/test_txn_8.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package internal

import (
"fmt"

"github.com/codecrafters-io/redis-tester/internal/redis_executable"
resp_connection "github.com/codecrafters-io/redis-tester/internal/resp/connection"
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/instrumented_resp_connection"
"github.com/codecrafters-io/redis-tester/internal/test_cases"
Expand All @@ -17,12 +21,17 @@ func testTxSuccess(stageHarness *test_case_harness.TestCaseHarness) error {

logger := stageHarness.Logger

client, err := instrumented_resp_connection.NewFromAddr(stageHarness, "localhost:6379", "client1")
if err != nil {
logFriendlyError(logger, err)
return err
var clients []*resp_connection.RespConnection

for i := 0; i < 2; i++ {
client, err := instrumented_resp_connection.NewFromAddr(stageHarness, "localhost:6379", fmt.Sprintf("client-%d", i+1))
if err != nil {
logFriendlyError(logger, err)
return err
}
clients = append(clients, client)
defer client.Close()
}
defer client.Close()

transactionTestCase := test_cases.TransactionTestCase{
CommandQueue: [][]string{
Expand All @@ -34,5 +43,15 @@ func testTxSuccess(stageHarness *test_case_harness.TestCaseHarness) error {
ResultArray: []resp_value.Value{resp_value.NewSimpleStringValue("OK"), resp_value.NewIntegerValue(7), resp_value.NewIntegerValue(1), resp_value.NewBulkStringValue("1")},
}

return transactionTestCase.RunAll(client, logger)
if err := transactionTestCase.RunAll(clients[0], logger); err != nil {
return err
}

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

return commandTestCase.Run(clients[1], logger)
}
14 changes: 13 additions & 1 deletion internal/test_txn_9.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,23 @@ func testTxDiscard(stageHarness *test_case_harness.TestCaseHarness) error {

logger := stageHarness.Logger

client, err := instrumented_resp_connection.NewFromAddr(stageHarness, "localhost:6379", "client1")
client, err := instrumented_resp_connection.NewFromAddr(stageHarness, "localhost:6379", "client")
if err != nil {
logFriendlyError(logger, err)
return err
}
defer client.Close()

commandTestCase := test_cases.SendCommandTestCase{
Command: "SET",
Args: []string{"bar", "42"},
Assertion: resp_assertions.NewStringAssertion("OK"),
}

if err := commandTestCase.Run(client, logger); err != nil {
return err
}

transactionTestCase := test_cases.TransactionTestCase{
CommandQueue: [][]string{
{"SET", "foo", "41"},
Expand All @@ -45,11 +55,13 @@ func testTxDiscard(stageHarness *test_case_harness.TestCaseHarness) error {
Commands: [][]string{
{"DISCARD"},
{"GET", "foo"},
{"GET", "bar"},
{"DISCARD"},
},
Assertions: []resp_assertions.RESPAssertion{
resp_assertions.NewStringAssertion("OK"),
resp_assertions.NewNilAssertion(),
resp_assertions.NewStringAssertion("42"),
resp_assertions.NewErrorAssertion("ERR DISCARD without MULTI"),
},
}
Expand Down
2 changes: 1 addition & 1 deletion internal/test_txn_empty.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func testTxEmpty(stageHarness *test_case_harness.TestCaseHarness) error {

logger := stageHarness.Logger

client, err := instrumented_resp_connection.NewFromAddr(stageHarness, "localhost:6379", "client1")
client, err := instrumented_resp_connection.NewFromAddr(stageHarness, "localhost:6379", "client")
if err != nil {
logFriendlyError(logger, err)
return err
Expand Down

0 comments on commit a5de8c0

Please sign in to comment.