Skip to content

Commit

Permalink
Testing the net_transport fast-path
Browse files Browse the repository at this point in the history
  • Loading branch information
armon committed Jan 6, 2015
1 parent 1f0e109 commit 493de35
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions net_transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,61 @@ func TestNetworkTransport_StartStop(t *testing.T) {
trans.Close()
}

func TestNetworkTransport_Heartbeat_FastPath(t *testing.T) {
// Transport 1 is consumer
trans1, err := NewTCPTransport("127.0.0.1:0", nil, 2, time.Second, nil)
if err != nil {
t.Fatalf("err: %v", err)
}
defer trans1.Close()

// Make the RPC request
args := AppendEntriesRequest{
Term: 10,
Leader: []byte("cartman"),
}
resp := AppendEntriesResponse{
Term: 4,
LastLog: 90,
Success: true,
}

invoked := false
fastpath := func(rpc RPC) {
// Verify the command
req := rpc.Command.(*AppendEntriesRequest)
if !reflect.DeepEqual(req, &args) {
t.Fatalf("command mismatch: %#v %#v", *req, args)
}

rpc.Respond(&resp, nil)
invoked = true
}
trans1.SetHeartbeatHandler(fastpath)

// Transport 2 makes outbound request
trans2, err := NewTCPTransport("127.0.0.1:0", nil, 2, time.Second, nil)
if err != nil {
t.Fatalf("err: %v", err)
}
defer trans2.Close()

var out AppendEntriesResponse
if err := trans2.AppendEntries(trans1.LocalAddr(), &args, &out); err != nil {
t.Fatalf("err: %v", err)
}

// Verify the response
if !reflect.DeepEqual(resp, out) {
t.Fatalf("command mismatch: %#v %#v", resp, out)
}

// Ensure fast-path is used
if !invoked {
t.Fatalf("fast-path not used")
}
}

func TestNetworkTransport_AppendEntries(t *testing.T) {
// Transport 1 is consumer
trans1, err := NewTCPTransport("127.0.0.1:0", nil, 2, time.Second, nil)
Expand Down

0 comments on commit 493de35

Please sign in to comment.