Skip to content

Commit

Permalink
Merge pull request #152 from codecrafters-io/CC-1292
Browse files Browse the repository at this point in the history
CC-1292: Update log prefix dynamically when reusing connection
  • Loading branch information
ryan-gang committed Jun 12, 2024
2 parents 5e242f8 + b6f96f0 commit 7d9020c
Show file tree
Hide file tree
Showing 6 changed files with 379 additions and 368 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,18 @@ import (

func defaultCallbacks(stageHarness *test_case_harness.TestCaseHarness, logPrefix string) resp_connection.RespConnectionCallbacks {
return resp_connection.RespConnectionCallbacks{
BeforeSendCommand: func(command string, args ...string) {
BeforeSendCommand: func(reusedConnection bool, command string, args ...string) {
var commandPrefix string
if reusedConnection {
commandPrefix = ">"
} else {
commandPrefix = "$ redis-cli"
}

if len(args) > 0 {
stageHarness.Logger.Infof("%s$ redis-cli %s %s", logPrefix, command, strings.Join(args, " "))
stageHarness.Logger.Infof("%s%s %s %s", logPrefix, commandPrefix, command, strings.Join(args, " "))
} else {
stageHarness.Logger.Infof("%s$ redis-cli %s", logPrefix, command)
stageHarness.Logger.Infof("%s%s %s", logPrefix, commandPrefix, command)
}
},
BeforeSendValue: func(value resp_value.Value) {
Expand Down
8 changes: 6 additions & 2 deletions internal/resp/connection/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
type RespConnectionCallbacks struct {
// BeforeSendCommand is called when a command is sent to the server.
// This can be useful for info logs.
BeforeSendCommand func(command string, args ...string)
BeforeSendCommand func(reusedConnection bool, command string, args ...string)

// BeforeSendValue is called when a value is sent using SendValue.
// It is NOT called when using SendCommand
Expand Down Expand Up @@ -83,7 +83,11 @@ func (c *RespConnection) Close() error {

func (c *RespConnection) SendCommand(command string, args ...string) error {
if c.Callbacks.BeforeSendCommand != nil {
c.Callbacks.BeforeSendCommand(command, args...)
if c.SentBytes > 0 {
c.Callbacks.BeforeSendCommand(true, command, args...)
} else {
c.Callbacks.BeforeSendCommand(false, command, args...)
}
}

encodedValue := resp_encoder.Encode(resp_value.NewStringArrayValue(append([]string{command}, args...)))
Expand Down
22 changes: 11 additions & 11 deletions internal/test_helpers/fixtures/expiry/pass
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ Debug = true
[stage-7] Received bytes: "+OK\r\n"
[stage-7] Received RESP simple string: "OK"
[stage-7] Received "OK"
[stage-7] Received OK at 20:39:57.019
[stage-7] Fetching key "apple" at 20:39:57.019 (should not be expired)
[stage-7] $ redis-cli GET apple
[stage-7] Received OK at 20:00:25.321
[stage-7] Fetching key "apple" at 20:00:25.321 (should not be expired)
[stage-7] > GET apple
[stage-7] Sent bytes: "*2\r\n$3\r\nGET\r\n$5\r\napple\r\n"
[stage-7] Received bytes: "$9\r\nblueberry\r\n"
[stage-7] Received RESP bulk string: "blueberry"
[stage-7] Received "blueberry"
[stage-7] Sleeping for 101ms
[stage-7] Fetching key "apple" at 20:39:57.122 (should be expired)
[stage-7] $ redis-cli GET apple
[stage-7] Fetching key "apple" at 20:00:25.425 (should be expired)
[stage-7] > GET apple
[stage-7] Sent bytes: "*2\r\n$3\r\nGET\r\n$5\r\napple\r\n"
[stage-7] Received bytes: "$-1\r\n"
[stage-7] Received RESP null bulk string: "$-1\r\n"
Expand All @@ -34,7 +34,7 @@ Debug = true
[stage-6] Received RESP simple string: "OK"
[stage-6] Received "OK"
[stage-6] Getting key blueberry
[stage-6] $ redis-cli GET blueberry
[stage-6] > GET blueberry
[stage-6] Sent bytes: "*2\r\n$3\r\nGET\r\n$9\r\nblueberry\r\n"
[stage-6] Received bytes: "$9\r\nraspberry\r\n"
[stage-6] Received RESP bulk string: "raspberry"
Expand Down Expand Up @@ -66,17 +66,17 @@ Debug = true
[stage-4] client-2: Received bytes: "+PONG\r\n"
[stage-4] client-2: Received RESP simple string: "PONG"
[stage-4] Received "PONG"
[stage-4] client-1: $ redis-cli PING
[stage-4] client-1: > PING
[stage-4] client-1: Sent bytes: "*1\r\n$4\r\nPING\r\n"
[stage-4] client-1: Received bytes: "+PONG\r\n"
[stage-4] client-1: Received RESP simple string: "PONG"
[stage-4] Received "PONG"
[stage-4] client-1: $ redis-cli PING
[stage-4] client-1: > PING
[stage-4] client-1: Sent bytes: "*1\r\n$4\r\nPING\r\n"
[stage-4] client-1: Received bytes: "+PONG\r\n"
[stage-4] client-1: Received RESP simple string: "PONG"
[stage-4] Received "PONG"
[stage-4] client-2: $ redis-cli PING
[stage-4] client-2: > PING
[stage-4] client-2: Sent bytes: "*1\r\n$4\r\nPING\r\n"
[stage-4] client-2: Received bytes: "+PONG\r\n"
[stage-4] client-2: Received RESP simple string: "PONG"
Expand All @@ -100,12 +100,12 @@ Debug = true
[stage-3] client-1: Received bytes: "+PONG\r\n"
[stage-3] client-1: Received RESP simple string: "PONG"
[stage-3] Received "PONG"
[stage-3] client-1: $ redis-cli PING
[stage-3] client-1: > PING
[stage-3] client-1: Sent bytes: "*1\r\n$4\r\nPING\r\n"
[stage-3] client-1: Received bytes: "+PONG\r\n"
[stage-3] client-1: Received RESP simple string: "PONG"
[stage-3] Received "PONG"
[stage-3] client-1: $ redis-cli PING
[stage-3] client-1: > PING
[stage-3] client-1: Sent bytes: "*1\r\n$4\r\nPING\r\n"
[stage-3] client-1: Received bytes: "+PONG\r\n"
[stage-3] client-1: Received RESP simple string: "PONG"
Expand Down
58 changes: 29 additions & 29 deletions internal/test_helpers/fixtures/rdb-read-value-with-expiry/pass
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
Debug = true

[stage-13] Running tests for Stage #13: sm4
[stage-13] $ ./spawn_redis_server.sh --dir /private/var/folders/5l/z5y3dkwn68sgb6htzc5w7vnm0000gn/T/rdbfiles388319618 --dbfilename pear.rdb
[stage-13] $ ./spawn_redis_server.sh --dir /private/var/folders/5l/z5y3dkwn68sgb6htzc5w7vnm0000gn/T/rdbfiles4019289137 --dbfilename pear.rdb
[stage-13] client: $ redis-cli GET orange
[stage-13] client: Sent bytes: "*2\r\n$3\r\nGET\r\n$6\r\norange\r\n"
[stage-13] client: Received bytes: "$10\r\nstrawberry\r\n"
[stage-13] client: Received RESP bulk string: "strawberry"
[stage-13] Received "strawberry"
[stage-13] client: $ redis-cli GET mango
[stage-13] client: > GET mango
[stage-13] client: Sent bytes: "*2\r\n$3\r\nGET\r\n$5\r\nmango\r\n"
[stage-13] client: Received bytes: "$-1\r\n"
[stage-13] client: Received RESP null bulk string: "$-1\r\n"
[stage-13] Received "$-1\r\n"
[stage-13] client: $ redis-cli GET pineapple
[stage-13] client: > GET pineapple
[stage-13] client: Sent bytes: "*2\r\n$3\r\nGET\r\n$9\r\npineapple\r\n"
[stage-13] client: Received bytes: "$9\r\nblueberry\r\n"
[stage-13] client: Received RESP bulk string: "blueberry"
Expand All @@ -23,28 +23,28 @@ Debug = true

[stage-12] Running tests for Stage #12: dq3
[stage-12] Created RDB file with key-value pairs: "grape"="pineapple", "apple"="raspberry", "banana"="apple", "mango"="orange", "orange"="pear"
[stage-12] $ ./spawn_redis_server.sh --dir /private/var/folders/5l/z5y3dkwn68sgb6htzc5w7vnm0000gn/T/rdbfiles2171782643 --dbfilename raspberry.rdb
[stage-12] $ ./spawn_redis_server.sh --dir /private/var/folders/5l/z5y3dkwn68sgb6htzc5w7vnm0000gn/T/rdbfiles117344466 --dbfilename raspberry.rdb
[stage-12] client: $ redis-cli GET grape
[stage-12] client: Sent bytes: "*2\r\n$3\r\nGET\r\n$5\r\ngrape\r\n"
[stage-12] client: Received bytes: "$9\r\npineapple\r\n"
[stage-12] client: Received RESP bulk string: "pineapple"
[stage-12] Received "pineapple"
[stage-12] client: $ redis-cli GET apple
[stage-12] client: > GET apple
[stage-12] client: Sent bytes: "*2\r\n$3\r\nGET\r\n$5\r\napple\r\n"
[stage-12] client: Received bytes: "$9\r\nraspberry\r\n"
[stage-12] client: Received RESP bulk string: "raspberry"
[stage-12] Received "raspberry"
[stage-12] client: $ redis-cli GET banana
[stage-12] client: > GET banana
[stage-12] client: Sent bytes: "*2\r\n$3\r\nGET\r\n$6\r\nbanana\r\n"
[stage-12] client: Received bytes: "$5\r\napple\r\n"
[stage-12] client: Received RESP bulk string: "apple"
[stage-12] Received "apple"
[stage-12] client: $ redis-cli GET mango
[stage-12] client: > GET mango
[stage-12] client: Sent bytes: "*2\r\n$3\r\nGET\r\n$5\r\nmango\r\n"
[stage-12] client: Received bytes: "$6\r\norange\r\n"
[stage-12] client: Received RESP bulk string: "orange"
[stage-12] Received "orange"
[stage-12] client: $ redis-cli GET orange
[stage-12] client: > GET orange
[stage-12] client: Sent bytes: "*2\r\n$3\r\nGET\r\n$6\r\norange\r\n"
[stage-12] client: Received bytes: "$4\r\npear\r\n"
[stage-12] client: Received RESP bulk string: "pear"
Expand All @@ -55,19 +55,19 @@ Debug = true

[stage-11] Running tests for Stage #11: jw4
[stage-11] Created RDB file with 3 keys: ["banana" "apple" "blueberry"]
[stage-11] $ ./spawn_redis_server.sh --dir /private/var/folders/5l/z5y3dkwn68sgb6htzc5w7vnm0000gn/T/rdbfiles1209284205 --dbfilename blueberry.rdb
[stage-11] $ ./spawn_redis_server.sh --dir /private/var/folders/5l/z5y3dkwn68sgb6htzc5w7vnm0000gn/T/rdbfiles2161578068 --dbfilename blueberry.rdb
[stage-11] client: $ redis-cli KEYS *
[stage-11] client: Sent bytes: "*2\r\n$4\r\nKEYS\r\n$1\r\n*\r\n"
[stage-11] client: Received bytes: "*3\r\n$5\r\napple\r\n$9\r\nblueberry\r\n$6\r\nbanana\r\n"
[stage-11] client: Received RESP array: ["apple", "blueberry", "banana"]
[stage-11] Received ["apple", "blueberry", "banana"]
[stage-11] client: Received bytes: "*3\r\n$9\r\nblueberry\r\n$6\r\nbanana\r\n$5\r\napple\r\n"
[stage-11] client: Received RESP array: ["blueberry", "banana", "apple"]
[stage-11] Received ["blueberry", "banana", "apple"]
[stage-11] Test passed.
[stage-11] Terminating program
[stage-11] Program terminated successfully

[stage-10] Running tests for Stage #10: gc6
[stage-10] Created RDB file with single key-value pair: grape="pineapple"
[stage-10] $ ./spawn_redis_server.sh --dir /private/var/folders/5l/z5y3dkwn68sgb6htzc5w7vnm0000gn/T/rdbfiles384942019 --dbfilename orange.rdb
[stage-10] $ ./spawn_redis_server.sh --dir /private/var/folders/5l/z5y3dkwn68sgb6htzc5w7vnm0000gn/T/rdbfiles2004705524 --dbfilename orange.rdb
[stage-10] client: $ redis-cli GET grape
[stage-10] client: Sent bytes: "*2\r\n$3\r\nGET\r\n$5\r\ngrape\r\n"
[stage-10] client: Received bytes: "$9\r\npineapple\r\n"
Expand All @@ -79,7 +79,7 @@ Debug = true

[stage-9] Running tests for Stage #9: jz6
[stage-9] Created RDB file with single key: "mango"
[stage-9] $ ./spawn_redis_server.sh --dir /private/var/folders/5l/z5y3dkwn68sgb6htzc5w7vnm0000gn/T/rdbfiles3198695930 --dbfilename pear.rdb
[stage-9] $ ./spawn_redis_server.sh --dir /private/var/folders/5l/z5y3dkwn68sgb6htzc5w7vnm0000gn/T/rdbfiles1043361946 --dbfilename pear.rdb
[stage-9] client: $ redis-cli KEYS *
[stage-9] client: Sent bytes: "*2\r\n$4\r\nKEYS\r\n$1\r\n*\r\n"
[stage-9] client: Received bytes: "*1\r\n$5\r\nmango\r\n"
Expand All @@ -90,12 +90,12 @@ Debug = true
[stage-9] Program terminated successfully

[stage-8] Running tests for Stage #8: zg5
[stage-8] $ ./spawn_redis_server.sh --dir /private/var/folders/5l/z5y3dkwn68sgb6htzc5w7vnm0000gn/T/rdbfiles825729594 --dbfilename blueberry.rdb
[stage-8] $ ./spawn_redis_server.sh --dir /private/var/folders/5l/z5y3dkwn68sgb6htzc5w7vnm0000gn/T/rdbfiles1967874176 --dbfilename blueberry.rdb
[stage-8] client: $ redis-cli CONFIG GET dir
[stage-8] client: Sent bytes: "*3\r\n$6\r\nCONFIG\r\n$3\r\nGET\r\n$3\r\ndir\r\n"
[stage-8] client: Received bytes: "*2\r\n$3\r\ndir\r\n$74\r\n/private/var/folders/5l/z5y3dkwn68sgb6htzc5w7vnm0000gn/T/rdbfiles825729594\r\n"
[stage-8] client: Received RESP array: ["dir", "/private/var/folders/5l/z5y3dkwn68sgb6htzc5w7vnm0000gn/T/rdbfiles825729594"]
[stage-8] Received ["dir", "/private/var/folders/5l/z5y3dkwn68sgb6htzc5w7vnm0000gn/T/rdbfiles825729594"]
[stage-8] client: Received bytes: "*2\r\n$3\r\ndir\r\n$75\r\n/private/var/folders/5l/z5y3dkwn68sgb6htzc5w7vnm0000gn/T/rdbfiles1967874176\r\n"
[stage-8] client: Received RESP array: ["dir", "/private/var/folders/5l/z5y3dkwn68sgb6htzc5w7vnm0000gn/T/rdbfiles1967874176"]
[stage-8] Received ["dir", "/private/var/folders/5l/z5y3dkwn68sgb6htzc5w7vnm0000gn/T/rdbfiles1967874176"]
[stage-8] Test passed.
[stage-8] Terminating program
[stage-8] Program terminated successfully
Expand All @@ -107,16 +107,16 @@ Debug = true
[stage-7] Received bytes: "+OK\r\n"
[stage-7] Received RESP simple string: "OK"
[stage-7] Received "OK"
[stage-7] Received OK at 20:40:46.013
[stage-7] Fetching key "strawberry" at 20:40:46.013 (should not be expired)
[stage-7] $ redis-cli GET strawberry
[stage-7] Received OK at 20:00:45.594
[stage-7] Fetching key "strawberry" at 20:00:45.594 (should not be expired)
[stage-7] > GET strawberry
[stage-7] Sent bytes: "*2\r\n$3\r\nGET\r\n$10\r\nstrawberry\r\n"
[stage-7] Received bytes: "$4\r\npear\r\n"
[stage-7] Received RESP bulk string: "pear"
[stage-7] Received "pear"
[stage-7] Sleeping for 101ms
[stage-7] Fetching key "strawberry" at 20:40:46.116 (should be expired)
[stage-7] $ redis-cli GET strawberry
[stage-7] Fetching key "strawberry" at 20:00:45.697 (should be expired)
[stage-7] > GET strawberry
[stage-7] Sent bytes: "*2\r\n$3\r\nGET\r\n$10\r\nstrawberry\r\n"
[stage-7] Received bytes: "$-1\r\n"
[stage-7] Received RESP null bulk string: "$-1\r\n"
Expand All @@ -134,7 +134,7 @@ Debug = true
[stage-6] Received RESP simple string: "OK"
[stage-6] Received "OK"
[stage-6] Getting key grape
[stage-6] $ redis-cli GET grape
[stage-6] > GET grape
[stage-6] Sent bytes: "*2\r\n$3\r\nGET\r\n$5\r\ngrape\r\n"
[stage-6] Received bytes: "$9\r\nraspberry\r\n"
[stage-6] Received RESP bulk string: "raspberry"
Expand Down Expand Up @@ -166,17 +166,17 @@ Debug = true
[stage-4] client-2: Received bytes: "+PONG\r\n"
[stage-4] client-2: Received RESP simple string: "PONG"
[stage-4] Received "PONG"
[stage-4] client-1: $ redis-cli PING
[stage-4] client-1: > PING
[stage-4] client-1: Sent bytes: "*1\r\n$4\r\nPING\r\n"
[stage-4] client-1: Received bytes: "+PONG\r\n"
[stage-4] client-1: Received RESP simple string: "PONG"
[stage-4] Received "PONG"
[stage-4] client-1: $ redis-cli PING
[stage-4] client-1: > PING
[stage-4] client-1: Sent bytes: "*1\r\n$4\r\nPING\r\n"
[stage-4] client-1: Received bytes: "+PONG\r\n"
[stage-4] client-1: Received RESP simple string: "PONG"
[stage-4] Received "PONG"
[stage-4] client-2: $ redis-cli PING
[stage-4] client-2: > PING
[stage-4] client-2: Sent bytes: "*1\r\n$4\r\nPING\r\n"
[stage-4] client-2: Received bytes: "+PONG\r\n"
[stage-4] client-2: Received RESP simple string: "PONG"
Expand All @@ -200,12 +200,12 @@ Debug = true
[stage-3] client-1: Received bytes: "+PONG\r\n"
[stage-3] client-1: Received RESP simple string: "PONG"
[stage-3] Received "PONG"
[stage-3] client-1: $ redis-cli PING
[stage-3] client-1: > PING
[stage-3] client-1: Sent bytes: "*1\r\n$4\r\nPING\r\n"
[stage-3] client-1: Received bytes: "+PONG\r\n"
[stage-3] client-1: Received RESP simple string: "PONG"
[stage-3] Received "PONG"
[stage-3] client-1: $ redis-cli PING
[stage-3] client-1: > PING
[stage-3] client-1: Sent bytes: "*1\r\n$4\r\nPING\r\n"
[stage-3] client-1: Received bytes: "+PONG\r\n"
[stage-3] client-1: Received RESP simple string: "PONG"
Expand Down

0 comments on commit 7d9020c

Please sign in to comment.