dice icon indicating copy to clipboard operation
dice copied to clipboard

Tests for command `PING`

Open arpitbbhayani opened this issue 3 years ago • 4 comments

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.

  1. covers the correctness of single command
  2. covers the correctness when multiple commands are fired
  3. no commands are dropped abruptly
  4. ensure we check for small, large, and massive inputs (if applicable)

arpitbbhayani avatar Oct 31 '22 14:10 arpitbbhayani

Hi, I will take up this issue.

harin-ramesh avatar Nov 05 '22 13:11 harin-ramesh

Hi, I am getting a connection refused error while running test case, seems like PING commands are properly working.

Screenshot 2022-11-06 at 7 45 47 PM

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()

harin-ramesh avatar Nov 06 '22 14:11 harin-ramesh

@HarinRamesh I'll have a look at this in some time!

rohanverma94 avatar Nov 06 '22 15:11 rohanverma94

@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()
}

rohanverma94 avatar Nov 06 '22 18:11 rohanverma94