Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

lock_test.gno

4.29 Kb · 124 lines
  1package arena
  2
  3import (
  4	"strconv"
  5	"strings"
  6	"testing"
  7
  8	"gno.land/p/nt/testutils/v0"
  9	"gno.land/p/nt/uassert/v0"
 10	"gno.land/p/nt/urequire/v0"
 11)
 12
 13var (
 14	keyOne   = testutils.TestAddress("key one")
 15	keyTwo   = testutils.TestAddress("key two")
 16	keyThree = testutils.TestAddress("key three")
 17)
 18
 19// showLocks lists locks as key:match:expiry, naming the test keys.
 20func showLocks(locks []sessionLock) string {
 21	names := map[address]string{keyOne: "one", keyTwo: "two", keyThree: "three"}
 22	var out []string
 23	for _, l := range locks {
 24		out = append(out, names[l.session]+":"+strconv.Itoa(l.id)+":"+strconv.FormatInt(l.expires, 10))
 25	}
 26	return strings.Join(out, " ")
 27}
 28
 29func TestUnlockedSessionLocks(t *testing.T) {
 30	for _, opening := range []bool{true, false} {
 31		locks, changed, refusal := lockSession(nil, keyOne, 2000, 3, opening, 1000)
 32		uassert.Equal(t, "", refusal)
 33		uassert.True(t, changed)
 34		uassert.Equal(t, "one:3:2000", showLocks(locks))
 35	}
 36	locks, changed, refusal := lockSession([]sessionLock{{keyTwo, 4, 2000}}, keyOne, 2000, 3, false, 1000)
 37	uassert.Equal(t, "", refusal)
 38	uassert.True(t, changed)
 39	uassert.Equal(t, "two:4:2000 one:3:2000", showLocks(locks))
 40}
 41
 42func TestLockedSessionRefusedElsewhere(t *testing.T) {
 43	locked := []sessionLock{{keyOne, 3, 2000}}
 44	_, _, refusal := lockSession(locked, keyOne, 2000, 5, false, 1000)
 45	uassert.Equal(t, "session locked to match 3", refusal)
 46
 47	locks, changed, refusal := lockSession(locked, keyOne, 2000, 3, false, 1000)
 48	uassert.Equal(t, "", refusal)
 49	uassert.False(t, changed)
 50	uassert.Equal(t, "one:3:2000", showLocks(locks))
 51}
 52
 53func TestLockedSessionCannotOpen(t *testing.T) {
 54	locked := []sessionLock{{keyOne, 3, 2000}}
 55	// Match 3 itself is refused too: a session cannot accept the challenge it posted.
 56	for _, id := range []int{3, 7} {
 57		_, _, refusal := lockSession(locked, keyOne, 2000, id, true, 1000)
 58		uassert.Equal(t, "session locked to match 3", refusal, strconv.Itoa(id))
 59	}
 60}
 61
 62func TestExpiredLockDropped(t *testing.T) {
 63	locks := []sessionLock{{keyOne, 3, 1000}, {keyTwo, 4, 1001}}
 64	got, changed, refusal := lockSession(locks, keyThree, 2000, 5, true, 1000)
 65	uassert.Equal(t, "", refusal)
 66	// One lock dropped and one added keep the count, and still change the list.
 67	uassert.True(t, changed)
 68	uassert.Equal(t, "two:4:1001 three:5:2000", showLocks(got))
 69
 70	got, changed, refusal = lockSession(locks, keyTwo, 1001, 4, false, 1000)
 71	uassert.Equal(t, "", refusal)
 72	uassert.True(t, changed)
 73	uassert.Equal(t, "two:4:1001", showLocks(got))
 74}
 75
 76func TestLockWithoutExpiry(t *testing.T) {
 77	const later = int64(2_000_000_000)
 78	locks := []sessionLock{{keyOne, 3, 0}}
 79	got, _, refusal := lockSession(locks, keyTwo, 0, 4, true, later)
 80	uassert.Equal(t, "", refusal)
 81	uassert.Equal(t, "one:3:0 two:4:0", showLocks(got))
 82
 83	_, _, refusal = lockSession(got, keyOne, 0, 5, false, later)
 84	uassert.Equal(t, "session locked to match 3", refusal)
 85}
 86
 87func TestReusedKeyStartsUnlocked(t *testing.T) {
 88	locks := []sessionLock{{keyOne, 3, 2000}, {keyTwo, 4, 2000}}
 89	got, changed, refusal := lockSession(locks, keyOne, 2500, 5, true, 1000)
 90	uassert.Equal(t, "", refusal)
 91	uassert.True(t, changed)
 92	uassert.Equal(t, "two:4:2000 one:5:2500", showLocks(got))
 93}
 94
 95// This pins the flag guard saves the player record on: a move in the session's own match
 96// changes nothing, so it writes nothing.
 97func TestLockReportsNoChangeInOwnMatch(t *testing.T) {
 98	p := playerRecord{ids: []int{3, 4}, locks: []sessionLock{{keyOne, 3, 2000}, {keyTwo, 4, 2000}}}
 99	uassert.False(t, p.lock(keyOne, 2000, 3, false, 1000))
100	uassert.Equal(t, "one:3:2000 two:4:2000", showLocks(p.locks))
101
102	uassert.True(t, p.lock(keyThree, 2000, 5, true, 1000))
103	uassert.Equal(t, "one:3:2000 two:4:2000 three:5:2000", showLocks(p.locks))
104
105	uassert.PanicsWithMessage(t, nil, "session locked to match 3", func() { p.lock(keyOne, 2000, 4, false, 1000) })
106}
107
108func TestLocksQuery(cur realm, t *testing.T) {
109	reset()
110	p := loadPlayer(alice)
111	p.add(3)
112	p.locks = []sessionLock{{keyOne, 3, 2000}, {keyTwo, 12, 0}}
113	savePlayer(alice, p)
114
115	got := Locks(alice)
116	urequire.Equal(t, 2, len(got))
117	uassert.Equal(t, keyOne.String(), got[0].Session)
118	uassert.Equal(t, "3", got[0].Match)
119	uassert.Equal(t, int64(2000), got[0].Expires)
120	uassert.Equal(t, keyTwo.String(), got[1].Session)
121	uassert.Equal(t, "12", got[1].Match)
122	uassert.Equal(t, int64(0), got[1].Expires)
123	uassert.Equal(t, 0, len(Locks(bob)))
124}