Skip to content

Commit

Permalink
Add Repl-10 test. (Draft).
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-gang committed Jan 31, 2024
1 parent 68721f3 commit 44b63ba
Showing 1 changed file with 108 additions and 0 deletions.
108 changes: 108 additions & 0 deletions internal/test_repl_master_psync_rdb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package internal

import (
"bufio"
"encoding/hex"
"fmt"
"net"
"strings"
"time"

testerutils "github.com/codecrafters-io/tester-utils"
"github.com/smallnest/resp3"
)

func testReplMasterPsyncRdb(stageHarness *testerutils.StageHarness) error {
master := NewRedisBinary(stageHarness)
master.args = []string{
"--port", "6379",
}

if err := master.Run(); err != nil {
return err
}

logger := stageHarness.Logger

client := NewRedisClient()

logger.Infof("$ redis-cli PING")
resp, err := client.Do("PING").Result()

if err != nil {
logFriendlyError(logger, err)
return err
}

if resp != "PONG" {
return fmt.Errorf("Expected OK from Master, received %v", resp)
}
logger.Successf("PONG received.")

logger.Infof("$ redis-cli REPLCONF listening-port 6380")
resp, err = client.Do("REPLCONF", "listening-port", "6380").Result()

if err != nil {
logFriendlyError(logger, err)
return err
}

if resp != "OK" {
return fmt.Errorf("Expected OK from Master, received %v", resp)
}
logger.Successf("OK received.")

conn, err := net.Dial("tcp", ":6379")
if err != nil {
fmt.Println("Error connecting to TCP server:", err)
}
defer conn.Close()

r := resp3.NewReader(conn)
w := resp3.NewWriter(conn)

w.WriteCommand("PSYNC", "?", "-1")
response, _, _ := r.ReadValue()
message := response.SmartResult()
respStr, _ := message.(string)
respParts := strings.Split(respStr, " ")
command := respParts[0]
offset := respParts[2]

if command != "FULLRESYNC" {
return fmt.Errorf("Expected FULLRESYNC from Master, received %v", command)
}
logger.Successf("FULLRESYNC received.")
if offset != "0" {
return fmt.Errorf("Expected offset to be 0 from Master, received %v", offset)
}
logger.Successf("offset = 0 received.")

reader := bufio.NewReader(conn)
var data []byte
timeout := 5 * time.Second

for {
conn.SetReadDeadline(time.Now().Add(timeout))

b, err := reader.ReadByte()
if err != nil {
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
break
}
}
data = append(data, b)
}

dataString := string(data)[6:]
// stringIOReader := strings.NewReader(dataString)
hexDigest := hex.EncodeToString([]byte(dataString))
fmt.Println(hexDigest)
// fmt.Println(dataString)
// decoder := core.NewDecoder(stringIOReader)
// fmt.Println(decoder.CheckHeader())

logger.Successf("RDB file received from master.")
client.Close()
return nil
}

0 comments on commit 44b63ba

Please sign in to comment.