Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
feat(BUX-222): broadcasting tests with go-broadcast-client
Browse files Browse the repository at this point in the history
  • Loading branch information
kuba-4chain committed Sep 29, 2023
1 parent 7a17c56 commit 506ec77
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 5 deletions.
108 changes: 105 additions & 3 deletions chainstate/broadcast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"context"
"strings"
"testing"
"time"

broadcast_client_mock "github.com/bitcoin-sv/go-broadcast-client/broadcast/broadcast-client-mock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tonicpow/go-minercraft/v2"
Expand Down Expand Up @@ -32,21 +34,92 @@ func TestClient_Broadcast_Success(t *testing.T) {
t.Parallel()

t.Run("broadcast - success (mAPI)", func(t *testing.T) {
// given
bc := broadcast_client_mock.Builder().
WithMockArc(broadcast_client_mock.MockSuccess).
Build()
c := NewTestClient(
context.Background(), t,
WithMinercraft(&minerCraftBroadcastSuccess{}),
WithBroadcastClient(bc),
)

// when
providers, err := c.Broadcast(
context.Background(), broadcastExample1TxID, broadcastExample1TxHex, defaultBroadcastTimeOut,
)

// then
require.NoError(t, err)
miners := strings.Split(providers, ",")
assert.GreaterOrEqual(t, len(miners), 1)
assert.True(t, containsAtLeastOneElement(
miners,
minercraft.MinerTaal,
minercraft.MinerMempool,
minercraft.MinerGorillaPool,
minercraft.MinerMatterpool,
ProviderBroadcastClient,
))
})

t.Run("broadcast - success (mAPI timeouts)", func(t *testing.T) {
// given
bc := broadcast_client_mock.Builder().
WithMockArc(broadcast_client_mock.MockSuccess).
Build()
c := NewTestClient(
context.Background(), t,
WithMinercraft(&minerCraftBroadcastTimeout{}), // Timeout
WithBroadcastClient(bc), // Success
)

// when
providers, err := c.Broadcast(
context.Background(), broadcastExample1TxID, broadcastExample1TxHex, defaultBroadcastTimeOut,
)

// then
require.NoError(t, err)
miners := strings.Split(providers, ",")
assert.GreaterOrEqual(t, len(miners), 1)
assert.True(t, containsAtLeastOneElement(miners, ProviderBroadcastClient))
assert.NotContains(t, miners, minercraft.MinerTaal)
assert.NotContains(t, miners, minercraft.MinerMempool)
assert.NotContains(t, miners, minercraft.MinerGorillaPool)
assert.NotContains(t, miners, minercraft.MinerMatterpool)
})

t.Run("broadcast - success (broadcastClient timeouts)", func(t *testing.T) {
// given
bc := broadcast_client_mock.Builder().
WithMockArc(broadcast_client_mock.MockTimeout).
Build()
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
c := NewTestClient(
ctx, t,
WithMinercraft(&minerCraftBroadcastSuccess{}), // Success
WithBroadcastClient(bc), // Timeout
)

assert.True(t, containsAtLeastOneElement(miners, minercraft.MinerTaal, minercraft.MinerMempool, minercraft.MinerGorillaPool, minercraft.MinerMatterpool))
// when
providers, err := c.Broadcast(
context.Background(), broadcastExample1TxID, broadcastExample1TxHex, defaultBroadcastTimeOut,
)

assert.NotContains(t, miners, ProviderWhatsOnChain)
// then
require.NoError(t, err)
miners := strings.Split(providers, ",")
assert.GreaterOrEqual(t, len(miners), 1)
assert.True(t, containsAtLeastOneElement(
miners,
minercraft.MinerTaal,
minercraft.MinerMempool,
minercraft.MinerGorillaPool,
minercraft.MinerMatterpool,
))
assert.NotContains(t, miners, ProviderBroadcastClient)
})
}

Expand All @@ -55,13 +128,18 @@ func TestClient_Broadcast_OnChain(t *testing.T) {
t.Parallel()

t.Run("broadcast - tx already on-chain (mAPI)", func(t *testing.T) {
// given
c := NewTestClient(
context.Background(), t,
WithMinercraft(&minerCraftTxOnChain{}),
)

// when
provider, err := c.Broadcast(
context.Background(), onChainExample1TxID, onChainExample1TxHex, defaultBroadcastTimeOut,
)

// then
require.NoError(t, err)
assert.NotEmpty(t, provider)
})
Expand All @@ -72,13 +150,18 @@ func TestClient_Broadcast_InMempool(t *testing.T) {
t.Parallel()

t.Run("broadcast - in mempool (mAPI)", func(t *testing.T) {
// given
c := NewTestClient(
context.Background(), t,
WithMinercraft(&minerCraftInMempool{}),
)

// when
provider, err := c.Broadcast(
context.Background(), onChainExample1TxID, onChainExample1TxHex, defaultBroadcastTimeOut,
)

// then
require.NoError(t, err)
assert.NotEmpty(t, provider)
})
Expand All @@ -89,33 +172,52 @@ func TestClient_Broadcast(t *testing.T) {
t.Parallel()

t.Run("error - missing tx id", func(t *testing.T) {
// given
c := NewTestClient(context.Background(), t)

// when
provider, err := c.Broadcast(
context.Background(), "", onChainExample1TxHex, defaultBroadcastTimeOut,
)

// then
require.Error(t, err)
assert.ErrorIs(t, err, ErrInvalidTransactionID)
assert.Empty(t, provider)
})

t.Run("error - missing tx hex", func(t *testing.T) {
// given
c := NewTestClient(context.Background(), t)

// when
provider, err := c.Broadcast(
context.Background(), onChainExample1TxID, "", defaultBroadcastTimeOut,
)

// then
require.Error(t, err)
assert.ErrorIs(t, err, ErrInvalidTransactionHex)
assert.Empty(t, provider)
})

t.Run("broadcast - all providers fail", func(t *testing.T) {
// given
bc := broadcast_client_mock.Builder().
WithMockArc(broadcast_client_mock.MockFailure).
Build()
c := NewTestClient(
context.Background(), t,
WithMinercraft(&minerCraftTxNotFound{}), // Not Found
WithMinercraft(&minerCraftTxNotFound{}),
WithBroadcastClient(bc),
)

// when
provider, err := c.Broadcast(
context.Background(), broadcastExample1TxID, broadcastExample1TxHex, defaultBroadcastTimeOut,
)

// then
require.Error(t, err)
assert.Equal(t, ProviderAll, provider)
})
Expand Down
2 changes: 0 additions & 2 deletions chainstate/chainstate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (

// NewTestClient returns a test client
func NewTestClient(ctx context.Context, t *testing.T, opts ...ClientOps) ClientInterface {
// WithMinercraft() is at the beginning so that it is possible to override minercraft's value with opts
opts = append([]ClientOps{WithMinercraft(&MinerCraftBase{})}, opts...)
c, err := NewClient(
ctx, append(opts, WithDebugging())...,
)
Expand Down

0 comments on commit 506ec77

Please sign in to comment.