Skip to content

Commit

Permalink
Update close test and examples.
Browse files Browse the repository at this point in the history
* Update the unit test to use a longer timeout for the
context. This test should now finish quickly and no longer
waits for the context to timeout.

* also add more logging on the example server.
  • Loading branch information
Aravind Srinivasan committed Feb 23, 2016
1 parent 21efc24 commit 923bd21
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
7 changes: 4 additions & 3 deletions close_test.go
Expand Up @@ -414,7 +414,6 @@ func TestCloseOneSide(t *testing.T) {
// TestCloseSendError tests that system errors are not attempted to be sent when
// a connection is closed, and ensures there's no race conditions such as the error
// frame being added to the channel just as it is closed.
// TODO(prashant): This test is waiting for timeout, but socket close shouldn't wait for timeout.
func TestCloseSendError(t *testing.T) {
closed := uint32(0)
counter := uint32(0)
Expand All @@ -433,14 +432,16 @@ func TestCloseSendError(t *testing.T) {
clientCh := testutils.NewClient(t, nil)

// Create a connection that will be shared.
require.NoError(t, testutils.Ping(clientCh, serverCh), "Ping from client to server failed")
// Use PingLong here so that we have a context with a bigger timeout
require.NoError(t, testutils.PingLong(clientCh, serverCh), "Ping from client to server failed")

var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(1)
go func() {
time.Sleep(time.Duration(rand.Intn(1000)) * time.Microsecond)
err := testutils.CallEcho(clientCh, serverCh, nil)
// Use CallEchoLong here so that we have a context with a bigger timeout
err := testutils.CallEchoLong(clientCh, serverCh, nil)
if err != nil && atomic.LoadUint32(&closed) == 0 {
t.Errorf("Call failed: %v", err)
}
Expand Down
5 changes: 5 additions & 0 deletions examples/server_crash/server.go
Expand Up @@ -70,7 +70,9 @@ func (Server) OpenPublisherStream(ctx thrift.Context, call server.BInOpenPublish
ack.Id = msg.Id
ack.Status = server.Status_OK

fmt.Println("Writing to ack channel")
ackChannel <- ack
fmt.Println("Wrote to ack channel")
}
}()
// Start the pump to send Acks back to the client
Expand All @@ -97,6 +99,9 @@ func (Server) OpenPublisherStream(ctx thrift.Context, call server.BInOpenPublish
if err := call.Write(ack); err != nil {
fmt.Printf("Server: Unable to Write Ack back to client for Id: %v, error: %v\n", ack.Id, err)
}
} else {
// AckChannel is closed
quit = true
}
}
}
Expand Down
22 changes: 22 additions & 0 deletions testutils/echo.go
Expand Up @@ -29,6 +29,20 @@ import (
"github.com/uber/tchannel-go/raw"
)

// CallEchoLong calls the "echo" endpoint from the given src to target.
func CallEchoLong(src, target *tchannel.Channel, args *raw.Args) error {
ctx, cancel := tchannel.NewContext(Timeout(30 * time.Second))
defer cancel()

if args == nil {
args = &raw.Args{}
}

peerInfo := target.PeerInfo()
_, _, _, err := raw.Call(ctx, src, peerInfo.HostPort, peerInfo.ServiceName, "echo", args.Arg2, args.Arg3)
return err
}

// CallEcho calls the "echo" endpoint from the given src to target.
func CallEcho(src, target *tchannel.Channel, args *raw.Args) error {
ctx, cancel := tchannel.NewContext(Timeout(100 * time.Millisecond))
Expand All @@ -54,6 +68,14 @@ func RegisterEcho(src *tchannel.Channel, f func()) {
})
}

// PingLong sends a ping from src to target.
func PingLong(src, target *tchannel.Channel) error {
ctx, cancel := tchannel.NewContext(Timeout(30 * time.Second))
defer cancel()

return src.Ping(ctx, target.PeerInfo().HostPort)
}

// Ping sends a ping from src to target.
func Ping(src, target *tchannel.Channel) error {
ctx, cancel := tchannel.NewContext(Timeout(100 * time.Millisecond))
Expand Down

0 comments on commit 923bd21

Please sign in to comment.