From d15dd7108765d4ae810dae6c5446b81ee76f3fbf Mon Sep 17 00:00:00 2001 From: ClaytonNorthey92 Date: Mon, 6 Nov 2023 12:08:54 -0500 Subject: [PATCH] added tests for GetBestBlockHashAsync now testing that GetBestBlockHashAsync sends the getbestblockhash command via websocket connection and that the channel returned can be used to send the response when it is received --- rpcclient/chain_test.go | 43 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) 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