From a46724e4f63d893b014644b6e11f67e30e97980d Mon Sep 17 00:00:00 2001 From: William Banfield <4561443+williambanfield@users.noreply.github.com> Date: Wed, 14 Jul 2021 10:16:09 -0400 Subject: [PATCH] statesync: dispatcher test uses internal channel for timing (#6713) This code change amends the dispatcher tests to read from the dispatcher's `requestCh`. This ensures that a request is waiting when the test calls `dispatcher.respond`. addresses: #6711 --- internal/statesync/dispatcher_test.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/internal/statesync/dispatcher_test.go b/internal/statesync/dispatcher_test.go index 33bc7c2b6..469630894 100644 --- a/internal/statesync/dispatcher_test.go +++ b/internal/statesync/dispatcher_test.go @@ -60,8 +60,8 @@ func TestDispatcherReturnsNoBlock(t *testing.T) { doneCh := make(chan struct{}) go func() { - err := d.respond(nil, peerFromSet) - require.Nil(t, err) + <-ch + require.NoError(t, d.respond(nil, peerFromSet)) close(doneCh) }() @@ -87,8 +87,8 @@ func TestDispatcherErrorsWhenNoPeers(t *testing.T) { func TestDispatcherReturnsBlockOncePeerAvailable(t *testing.T) { t.Cleanup(leaktest.Check(t)) - ch := make(chan p2p.Envelope, 100) - d := newDispatcher(ch, 1*time.Second) + dispatcherRequestCh := make(chan p2p.Envelope, 100) + d := newDispatcher(dispatcherRequestCh, 1*time.Second) peerFromSet := createPeerSet(1)[0] d.addPeer(peerFromSet) ctx := context.Background() @@ -100,12 +100,18 @@ func TestDispatcherReturnsBlockOncePeerAvailable(t *testing.T) { require.Nil(t, lb) require.Equal(t, peerFromSet, peerResult) require.Nil(t, err) + + // calls to dispatcher.Lightblock write into the dispatcher's requestCh. + // we read from the requestCh here to unblock the requestCh for future + // calls. + <-dispatcherRequestCh close(doneCh) }() cancelFunc() <-doneCh go func() { + <-dispatcherRequestCh lb := &types.LightBlock{} asProto, err := lb.ToProto() require.Nil(t, err)