add test to demonstrate failure

This commit is contained in:
Jeromy 2020-05-14 11:48:58 -07:00
parent d1997b5857
commit 987571dd8c

View File

@ -298,7 +298,7 @@ func (h *ChanHandler) Sub(ctx context.Context, i int, eq int) (<-chan int, error
for {
select {
case <-ctx.Done():
fmt.Println("ctxdone1")
fmt.Println("ctxdone1", i, eq)
return
case <-wait:
}
@ -393,6 +393,54 @@ func TestChan(t *testing.T) {
require.Equal(t, false, ok)
}
func TestChanClosing(t *testing.T) {
var client struct {
Sub func(context.Context, int, int) (<-chan int, error)
}
serverHandler := &ChanHandler{
wait: make(chan struct{}, 5),
}
rpcServer := NewServer()
rpcServer.Register("ChanHandler", serverHandler)
testServ := httptest.NewServer(rpcServer)
defer testServ.Close()
closer, err := NewClient("ws://"+testServ.Listener.Addr().String(), "ChanHandler", &client, nil)
require.NoError(t, err)
defer closer()
ctx1, cancel1 := context.WithCancel(context.Background())
ctx2, cancel2 := context.WithCancel(context.Background())
defer cancel2()
// sub
sub1, err := client.Sub(ctx1, 2, -1)
require.NoError(t, err)
sub2, err := client.Sub(ctx2, 3, -1)
require.NoError(t, err)
// recv one
serverHandler.wait <- struct{}{}
serverHandler.wait <- struct{}{}
require.Equal(t, 2, <-sub1)
require.Equal(t, 3, <-sub2)
cancel1()
serverHandler.wait <- struct{}{}
require.Equal(t, 0, <-sub1)
require.Equal(t, 6, <-sub2)
}
func TestChanServerClose(t *testing.T) {
var client struct {
Sub func(context.Context, int, int) (<-chan int, error)