github.com/MetalBlockchain/metalgo@v1.11.9/api/info/client_test.go (about) 1 // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. 2 // See the file LICENSE for licensing terms. 3 4 package info 5 6 import ( 7 "context" 8 "testing" 9 "time" 10 11 "github.com/stretchr/testify/require" 12 13 "github.com/MetalBlockchain/metalgo/utils/rpc" 14 ) 15 16 type mockClient struct { 17 reply IsBootstrappedResponse 18 err error 19 onCall func() 20 } 21 22 func (mc *mockClient) SendRequest(_ context.Context, _ string, _ interface{}, replyIntf interface{}, _ ...rpc.Option) error { 23 reply := replyIntf.(*IsBootstrappedResponse) 24 *reply = mc.reply 25 mc.onCall() 26 return mc.err 27 } 28 29 func TestNewClient(t *testing.T) { 30 require := require.New(t) 31 32 c := NewClient("") 33 require.NotNil(c) 34 } 35 36 func TestClient(t *testing.T) { 37 require := require.New(t) 38 39 mc := &mockClient{ 40 reply: IsBootstrappedResponse{true}, 41 err: nil, 42 onCall: func() {}, 43 } 44 c := &client{ 45 requester: mc, 46 } 47 48 { 49 bootstrapped, err := c.IsBootstrapped(context.Background(), "X") 50 require.NoError(err) 51 require.True(bootstrapped) 52 } 53 54 mc.reply.IsBootstrapped = false 55 56 { 57 bootstrapped, err := c.IsBootstrapped(context.Background(), "X") 58 require.NoError(err) 59 require.False(bootstrapped) 60 } 61 62 mc.onCall = func() { 63 mc.reply.IsBootstrapped = true 64 } 65 66 { 67 bootstrapped, err := AwaitBootstrapped(context.Background(), c, "X", time.Microsecond) 68 require.NoError(err) 69 require.True(bootstrapped) 70 } 71 }