monit_test.gno
2.55 Kb ยท 99 lines
1package monit
2
3import (
4 "testing"
5 "time"
6
7 "gno.land/p/nt/ownable"
8 "gno.land/p/nt/uassert"
9 "gno.land/p/nt/watchdog"
10)
11
12func initTest() {
13 counter = 0
14 lastUpdate = time.Time{}
15 lastCaller = address("")
16 wd = watchdog.Watchdog{Duration: watchdogDuration}
17 creator := address("g1creator")
18 Ownable = ownable.NewWithAddress(creator)
19}
20
21func TestPackage(t *testing.T) {
22 initTest()
23
24 testing.SetRealm(testing.NewUserRealm("g1user"))
25
26 // initial state, watchdog is KO.
27 {
28 expected := `counter=0
29last update=0001-01-01 00:00:00 +0000 UTC
30last caller=
31status=KO`
32 got := Render("")
33 uassert.Equal(t, expected, got)
34 }
35
36 // call Incr(), watchdog is OK.
37 Incr(cross)
38 Incr(cross)
39 Incr(cross)
40 {
41 expected := `counter=3
42last update=2009-02-13 23:31:30 +0000 UTC
43last caller=g1user
44status=OK`
45 got := Render("")
46 uassert.Equal(t, expected, got)
47 }
48
49 /* XXX: improve tests once we've the missing std.TestSkipTime feature
50 // wait 1h, watchdog is KO.
51 use std.TestSkipTime(time.Hour)
52 {
53 expected := `counter=3
54 last update=2009-02-13 22:31:30 +0000 UTC
55 last caller=g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm
56 status=KO`
57 got := Render("")
58 uassert.Equal(t, expected, got)
59 }
60
61 // call Incr(), watchdog is OK.
62 Incr()
63 {
64 expected := `counter=4
65 last update=2009-02-13 23:31:30 +0000 UTC
66 last caller=g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm
67 status=OK`
68 got := Render("")
69 uassert.Equal(t, expected, got)
70 }
71 */
72}
73
74func TestReset(t *testing.T) {
75 initTest()
76
77 // Initial state check
78 initialCounter := counter
79 initialLastUpdate := lastUpdate
80 initialStatus := wd.Status()
81
82 // Call Incr to change the state
83 user := address("g1user")
84 testing.SetRealm(testing.NewUserRealm(user))
85 Incr(cross)
86 uassert.True(t, counter > initialCounter, "counter should have increased after Incr")
87 uassert.True(t, lastUpdate.After(initialLastUpdate), "lastUpdate should have been updated after Incr")
88 uassert.Equal(t, user, lastCaller, "lastCaller mismatch")
89 uassert.NotEqual(t, initialStatus, wd.Status(), "watchdog status should have changed after Incr") // Status changes after Alive() is called
90
91 // Call Reset as the owner
92 ownerAddr := Ownable.Owner()
93 testing.SetRealm(testing.NewUserRealm(ownerAddr)) // Simulate call from the owner
94 Reset(cross)
95 uassert.Equal(t, 0, counter, "counter should be 0 after Reset")
96 uassert.Equal(t, ownerAddr, lastCaller, "lastCaller should be the owner address after Reset")
97 uassert.Equal(t, watchdogDuration.String(), wd.Duration.String(), "watchdog duration mismatch after Reset")
98 uassert.Equal(t, "KO", wd.Status(), "watchdog status should be KO after Reset")
99}