Skip to content

Commit

Permalink
added tests for GetBestBlockHashAsync
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ClaytonNorthey92 authored and jcvernaleo committed Nov 6, 2023
1 parent d988b86 commit d15dd71
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions rpcclient/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"time"
)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d15dd71

Please sign in to comment.