diff --git a/rpcclient/chain_test.go b/rpcclient/chain_test.go index cc29f99bfc..de8d3a740e 100644 --- a/rpcclient/chain_test.go +++ b/rpcclient/chain_test.go @@ -6,6 +6,7 @@ import ( "net/http" "net/http/httptest" "strings" + "sync" "testing" "time" ) @@ -190,6 +191,48 @@ func TestClientConnectedToWSServerRunner(t *testing.T) { } }, }, + TestTableItem{ + Name: "TestGetBestBlockHashAsync", + TestCase: func(t *testing.T) { + client, serverReceivedChannel, cleanup := makeClient(t) + defer cleanup() + ch := client.GetBestBlockHashAsync() + + message := <-serverReceivedChannel + if message != "{\"jsonrpc\":\"1.0\",\"method\":\"getbestblockhash\",\"params\":[],\"id\":1}" { + t.Fatalf("received unexpected message: %s", message) + } + + expectedResponse := Response{} + + wg := sync.WaitGroup{} + + wg.Add(1) + go func() { + defer wg.Done() + for { + client.requestLock.Lock() + if client.requestList.Len() > 0 { + r := client.requestList.Back() + r.Value.(*jsonRequest).responseChan <- &expectedResponse + client.requestLock.Unlock() + return + } + client.requestLock.Unlock() + } + }() + + response := <-ch + + if &expectedResponse != response { + t.Fatalf("received unexepcted response") + } + + // ensure the goroutine created in this test exists, + // the test is ran with a timeout + wg.Wait() + }, + }, } // since these tests rely on concurrency, ensure there is a resonable timeout