-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |