Tests for command `PING`
Currently, there are almost no unit tests for the PING command. We need a comprehensive test suite for this command that also checks the correctness and completeness.
- covers the correctness of single command
- covers the correctness when multiple commands are fired
- no commands are dropped abruptly
- ensure we check for small, large, and massive inputs (if applicable)
Hi, I will take up this issue.
Hi, I am getting a connection refused error while running test case, seems like PING commands are properly working.
Below is my code for the test. @arpitbbhayani @rohanverma94
var wg sync.WaitGroup
go runTestServer(&wg)
var b []byte
var buf = bytes.NewBuffer(b)
conn := getLocalConnection()
buf.WriteByte('a')
cmd := fmt.Sprintf("PING")
v := fireCommand(conn, cmd)
if v != "PONG" {
t.Fail()
}
fmt.Println("====================================")
fmt.Println(v)
fmt.Println("====================================")
cmd = fmt.Sprintf("PING HELLO!")
v = fireCommand(conn, cmd)
if v != "HELLO!" {
t.Fail()
}
fmt.Println("====================================")
fmt.Println(v)
fmt.Println("====================================")
fireCommand(conn, "ABORT")
wg.Wait()
@HarinRamesh I'll have a look at this in some time!
@HarinRamesh Thanks for reporting this! This is not an issue with PING, but it has to do with the organization of the TestSuite code. The client connection should provide a warmup to disallow any data race with server FD. The insight of this issue is below:
syscall.Listen(serverFD, maxClients)
and
net.Dial("tcp", "localhost:8379")
Both are called concurrently, and you can't predict which one executes first i.e. data race, it is a nondeterministic behavior
If Dial is called before Listen then there is obviously no listening socket yet and that produces the error.
@arpitbbhayani I propose following change-
//In runTestServer func , return an error channel also
func runTestServer(wg *sync.WaitGroup) error {
config.IOBufferLength = 16
config.Port = 8379
wg.Add(1)
return server.RunAsyncTCPServer(wg)
}
//In ping_test.go use that error channel to make sure that listen() is called before Dial()
func TestPING(t *testing.T) {
var wg sync.WaitGroup
go func() {
err := runTestServer(&wg)
if err != nil {
conn := getLocalConnection()
cmd := fmt.Sprintf("PING")
v := fireCommand(conn, cmd)
if v != "PONG" {
t.Fail()
}
fmt.Println("====================================")
fmt.Println(v)
fmt.Println("====================================")
cmd = fmt.Sprintf("PING HELLO!")
v = fireCommand(conn, cmd)
if v != "HELLO!" {
t.Fail()
}
fmt.Println("====================================")
fmt.Println(v)
fmt.Println("====================================")
fireCommand(conn, "ABORT")
wg.Done()
}
}()
wg.Wait()
}