Skip to content

Commit

Permalink
Standardize on ts.ServiceName()
Browse files Browse the repository at this point in the history
  • Loading branch information
Akshay Shah committed Jun 11, 2016
1 parent 3abb9d2 commit 241b8d2
Showing 1 changed file with 24 additions and 30 deletions.
54 changes: 24 additions & 30 deletions connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@ import (

// Values used in tests
var (
testServiceName = testutils.DefaultServerName
testArg2 = []byte("Header in arg2")
testArg3 = []byte("Body in arg3")
testArg2 = []byte("Header in arg2")
testArg3 = []byte("Body in arg3")
)

type testHandler struct {
Expand Down Expand Up @@ -114,7 +113,7 @@ func TestRoundTrip(t *testing.T) {
ctx, cancel := NewContext(time.Second)
defer cancel()

call, err := ts.Server().BeginCall(ctx, ts.HostPort(), testServiceName, "echo", &CallOptions{Format: JSON})
call, err := ts.Server().BeginCall(ctx, ts.HostPort(), ts.ServiceName(), "echo", &CallOptions{Format: JSON})
require.NoError(t, err)
assert.NotEmpty(t, call.RemotePeer().HostPort)

Expand All @@ -130,7 +129,7 @@ func TestRoundTrip(t *testing.T) {
assert.Equal(t, testArg3, []byte(respArg3))

assert.Equal(t, JSON, handler.format)
assert.Equal(t, testServiceName, handler.caller)
assert.Equal(t, ts.ServiceName(), handler.caller)
assert.Equal(t, JSON, call.Response().Format(), "response Format should match request Format")
})
}
Expand All @@ -143,7 +142,7 @@ func TestDefaultFormat(t *testing.T) {
ctx, cancel := NewContext(time.Second)
defer cancel()

arg2, arg3, resp, err := raw.Call(ctx, ts.Server(), ts.HostPort(), testServiceName, "echo", testArg2, testArg3)
arg2, arg3, resp, err := raw.Call(ctx, ts.Server(), ts.HostPort(), ts.ServiceName(), "echo", testArg2, testArg3)
require.Nil(t, err)

require.Equal(t, testArg2, arg2)
Expand Down Expand Up @@ -299,13 +298,12 @@ func TestBadRequest(t *testing.T) {
ctx, cancel := NewContext(time.Second)
defer cancel()

serviceName := ts.Server().ServiceName()
_, _, _, err := raw.Call(ctx, ts.Server(), ts.HostPort(), serviceName, "Noone", []byte("Headers"), []byte("Body"))
_, _, _, err := raw.Call(ctx, ts.Server(), ts.HostPort(), ts.ServiceName(), "Noone", []byte("Headers"), []byte("Body"))
require.NotNil(t, err)
assert.Equal(t, ErrCodeBadRequest, GetSystemErrorCode(err))

calls := relay.NewMockStats()
calls.Add(serviceName, serviceName, "Noone").Failed("bad-request").End()
calls.Add(ts.ServiceName(), ts.ServiceName(), "Noone").Failed("bad-request").End()
ts.AssertRelayStats(calls)
})
}
Expand Down Expand Up @@ -335,12 +333,12 @@ func TestServerBusy(t *testing.T) {
ctx, cancel := NewContext(time.Second)
defer cancel()

_, _, _, err := raw.Call(ctx, ts.Server(), ts.HostPort(), testServiceName, "busy", []byte("Arg2"), []byte("Arg3"))
_, _, _, err := raw.Call(ctx, ts.Server(), ts.HostPort(), ts.ServiceName(), "busy", []byte("Arg2"), []byte("Arg3"))
require.NotNil(t, err)
assert.Equal(t, ErrCodeBusy, GetSystemErrorCode(err), "err: %v", err)

calls := relay.NewMockStats()
calls.Add(testServiceName, ts.Server().ServiceName(), "busy").Failed("busy").End()
calls.Add(ts.ServiceName(), ts.ServiceName(), "busy").Failed("busy").End()
ts.AssertRelayStats(calls)
})
}
Expand All @@ -360,12 +358,12 @@ func TestUnexpectedHandlerError(t *testing.T) {
ctx, cancel := NewContext(time.Second)
defer cancel()

_, _, _, err := raw.Call(ctx, ts.Server(), ts.HostPort(), testServiceName, "nope", []byte("Arg2"), []byte("Arg3"))
_, _, _, err := raw.Call(ctx, ts.Server(), ts.HostPort(), ts.ServiceName(), "nope", []byte("Arg2"), []byte("Arg3"))
require.NotNil(t, err)
assert.Equal(t, ErrCodeUnexpected, GetSystemErrorCode(err), "err: %v", err)

calls := relay.NewMockStats()
calls.Add(testServiceName, ts.Server().ServiceName(), "nope").Failed("unexpected-error").End()
calls.Add(ts.ServiceName(), ts.ServiceName(), "nope").Failed("unexpected-error").End()
ts.AssertRelayStats(calls)
})
}
Expand All @@ -392,8 +390,7 @@ func TestTimeout(t *testing.T) {
ctx, cancel := NewContext(testutils.Timeout(15 * time.Millisecond))
defer cancel()

serviceName := ts.Server().ServiceName()
_, _, _, err := raw.Call(ctx, ts.Server(), ts.HostPort(), serviceName, "block", []byte("Arg2"), []byte("Arg3"))
_, _, _, err := raw.Call(ctx, ts.Server(), ts.HostPort(), ts.ServiceName(), "block", []byte("Arg2"), []byte("Arg3"))
assert.Equal(t, ErrTimeout, err)

// Verify the server-side receives an error from the context.
Expand All @@ -405,7 +402,7 @@ func TestTimeout(t *testing.T) {
}

calls := relay.NewMockStats()
calls.Add(serviceName, serviceName, "block").Failed("timeout").End()
calls.Add(ts.ServiceName(), ts.ServiceName(), "block").Failed("timeout").End()
ts.AssertRelayStats(calls)
})
}
Expand All @@ -416,7 +413,7 @@ func TestLargeMethod(t *testing.T) {
defer cancel()

largeMethod := testutils.RandBytes(16*1024 + 1)
_, _, _, err := raw.Call(ctx, ts.Server(), ts.HostPort(), testServiceName, string(largeMethod), nil, nil)
_, _, _, err := raw.Call(ctx, ts.Server(), ts.HostPort(), ts.ServiceName(), string(largeMethod), nil, nil)
assert.Equal(t, ErrMethodTooLarge, err)
})
}
Expand All @@ -428,11 +425,11 @@ func TestLargeTimeout(t *testing.T) {
ctx, cancel := NewContext(1000 * time.Second)
defer cancel()

_, _, _, err := raw.Call(ctx, ts.Server(), ts.HostPort(), testServiceName, "echo", testArg2, testArg3)
_, _, _, err := raw.Call(ctx, ts.Server(), ts.HostPort(), ts.ServiceName(), "echo", testArg2, testArg3)
assert.NoError(t, err, "Call failed")

calls := relay.NewMockStats()
calls.Add(testServiceName, testServiceName, "echo").Succeeded().End()
calls.Add(ts.ServiceName(), ts.ServiceName(), "echo").Succeeded().End()
ts.AssertRelayStats(calls)
})
}
Expand All @@ -454,13 +451,13 @@ func TestFragmentation(t *testing.T) {
ctx, cancel := NewContext(time.Second)
defer cancel()

respArg2, respArg3, _, err := raw.Call(ctx, ts.Server(), ts.HostPort(), testServiceName, "echo", arg2, arg3)
respArg2, respArg3, _, err := raw.Call(ctx, ts.Server(), ts.HostPort(), ts.ServiceName(), "echo", arg2, arg3)
require.NoError(t, err)
assert.Equal(t, arg2, respArg2)
assert.Equal(t, arg3, respArg3)

calls := relay.NewMockStats()
calls.Add(testServiceName, testServiceName, "echo").Succeeded().End()
calls.Add(ts.ServiceName(), ts.ServiceName(), "echo").Succeeded().End()
ts.AssertRelayStats(calls)
})
}
Expand Down Expand Up @@ -489,8 +486,7 @@ func TestFragmentationSlowReader(t *testing.T) {
ctx, cancel := NewContext(testutils.Timeout(30 * time.Millisecond))
defer cancel()

serviceName := ts.Server().PeerInfo().ServiceName
_, _, _, err := raw.Call(ctx, ts.Server(), ts.HostPort(), serviceName, "echo", arg2, arg3)
_, _, _, err := raw.Call(ctx, ts.Server(), ts.HostPort(), ts.ServiceName(), "echo", arg2, arg3)
assert.Error(t, err, "Call should timeout due to slow reader")

close(startReading)
Expand All @@ -501,7 +497,7 @@ func TestFragmentationSlowReader(t *testing.T) {
}

calls := relay.NewMockStats()
calls.Add(serviceName, serviceName, "echo").Failed("timeout").End()
calls.Add(ts.ServiceName(), ts.ServiceName(), "echo").Failed("timeout").End()
ts.AssertRelayStats(calls)
})
}
Expand Down Expand Up @@ -534,8 +530,7 @@ func TestWriteArg3AfterTimeout(t *testing.T) {
ctx, cancel := NewContext(testutils.Timeout(50 * time.Millisecond))
defer cancel()

serviceName := ts.Server().PeerInfo().ServiceName
_, _, _, err := raw.Call(ctx, ts.Server(), ts.HostPort(), serviceName, "call", nil, nil)
_, _, _, err := raw.Call(ctx, ts.Server(), ts.HostPort(), ts.ServiceName(), "call", nil, nil)
assert.Equal(t, err, ErrTimeout, "Call should timeout")

// Wait for the write to complete, make sure there's no errors.
Expand All @@ -546,7 +541,7 @@ func TestWriteArg3AfterTimeout(t *testing.T) {
}

calls := relay.NewMockStats()
calls.Add(serviceName, serviceName, "call").Failed("timeout").Succeeded().End()
calls.Add(ts.ServiceName(), ts.ServiceName(), "call").Failed("timeout").Succeeded().End()
ts.AssertRelayStats(calls)
})
}
Expand All @@ -567,10 +562,9 @@ func TestWriteErrorAfterTimeout(t *testing.T) {
}
ts.Register(HandlerFunc(handler), "call")

serviceName := ts.Server().PeerInfo().ServiceName
ctx, cancel := NewContext(testutils.Timeout(30 * time.Millisecond))
defer cancel()
_, _, _, err := raw.Call(ctx, ts.Server(), ts.HostPort(), serviceName, "call", nil, testutils.RandBytes(100000))
_, _, _, err := raw.Call(ctx, ts.Server(), ts.HostPort(), ts.ServiceName(), "call", nil, testutils.RandBytes(100000))
assert.Equal(t, err, ErrTimeout, "Call should timeout")
close(timedOut)

Expand All @@ -581,7 +575,7 @@ func TestWriteErrorAfterTimeout(t *testing.T) {
}

calls := relay.NewMockStats()
calls.Add(serviceName, serviceName, "call").Failed("timeout").End()
calls.Add(ts.ServiceName(), ts.ServiceName(), "call").Failed("timeout").End()
ts.AssertRelayStats(calls)
})
}
Expand Down

0 comments on commit 241b8d2

Please sign in to comment.