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