forked from akash-network/provider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
watchdog_test.go
118 lines (93 loc) · 3.44 KB
/
watchdog_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package manifest
import (
"testing"
"time"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
sdk "github.com/cosmos/cosmos-sdk/types"
broadcastmocks "github.com/akash-network/node/client/broadcaster/mocks"
clientmocks "github.com/akash-network/node/client/mocks"
"github.com/akash-network/node/testutil"
dtypes "github.com/akash-network/node/x/deployment/types/v1beta2"
types "github.com/akash-network/node/x/market/types/v1beta2"
ptypes "github.com/akash-network/node/x/provider/types/v1beta2"
"github.com/akash-network/provider/session"
)
type watchdogTestScaffold struct {
client *clientmocks.Client
parentCh chan struct{}
doneCh chan dtypes.DeploymentID
broadcasts chan sdk.Msg
leaseID types.LeaseID
provider ptypes.Provider
}
func makeWatchdogTestScaffold(t *testing.T, timeout time.Duration) (*watchdog, *watchdogTestScaffold) {
scaffold := &watchdogTestScaffold{}
scaffold.parentCh = make(chan struct{})
scaffold.doneCh = make(chan dtypes.DeploymentID, 1)
scaffold.broadcasts = make(chan sdk.Msg)
scaffold.provider = testutil.Provider(t)
scaffold.leaseID = testutil.LeaseID(t)
scaffold.leaseID.Provider = scaffold.provider.Owner
scaffold.broadcasts = make(chan sdk.Msg, 1)
txClientMock := &broadcastmocks.Client{}
txClientMock.On("Broadcast", mock.Anything, mock.Anything).Run(func(args mock.Arguments) {
scaffold.broadcasts <- args.Get(1).(sdk.Msg)
}).Return(nil)
scaffold.client = &clientmocks.Client{}
scaffold.client.On("Tx").Return(txClientMock)
sess := session.New(testutil.Logger(t), scaffold.client, &scaffold.provider, -1)
require.NotNil(t, sess.Client())
wd := newWatchdog(sess, scaffold.parentCh, scaffold.doneCh, scaffold.leaseID, timeout)
return wd, scaffold
}
func TestWatchdogTimesout(t *testing.T) {
wd, scaffold := makeWatchdogTestScaffold(t, 3*time.Second)
select {
case <-wd.lc.Done():
case <-time.After(10 * time.Second):
t.Fatal("timed out waiting on watchdog to stop")
}
// Check that close bid was sent
msg := testutil.ChannelWaitForValue(t, scaffold.broadcasts)
closeBid, ok := msg.(*types.MsgCloseBid)
require.True(t, ok)
require.Equal(t, closeBid.BidID.LeaseID(), scaffold.leaseID)
deploymentID := testutil.ChannelWaitForValue(t, scaffold.doneCh)
require.Equal(t, deploymentID, scaffold.leaseID.DeploymentID())
}
func TestWatchdogStops(t *testing.T) {
wd, scaffold := makeWatchdogTestScaffold(t, 1*time.Minute)
wd.stop() // ask it to stop immediately
wd.stop() // ask it to stop a second time, this is expected usage
select {
case <-wd.lc.Done():
case <-time.After(10 * time.Second):
t.Fatal("timed out waiting on watchdog to stop")
}
// Check that close bid was not sent
select {
case <-scaffold.broadcasts:
t.Fatal("should no have broadcast any message")
default:
}
deploymentID := testutil.ChannelWaitForValue(t, scaffold.doneCh)
require.Equal(t, deploymentID, scaffold.leaseID.DeploymentID())
}
func TestWatchdogStopsOnParent(t *testing.T) {
wd, scaffold := makeWatchdogTestScaffold(t, 1*time.Minute)
close(scaffold.parentCh) // ask it to stop immediately
select {
case <-wd.lc.Done():
case <-time.After(10 * time.Second):
t.Fatal("timed out waiting on watchdog to stop")
}
// Check that close bid was not sent
select {
case <-scaffold.broadcasts:
t.Fatal("should no have broadcast any message")
default:
}
deploymentID := testutil.ChannelWaitForValue(t, scaffold.doneCh)
require.Equal(t, deploymentID, scaffold.leaseID.DeploymentID())
}