Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

impl_test.gno

4.23 Kb · 111 lines
  1package impl
  2
  3import (
  4	"testing"
  5
  6	facade "gno.land/r/moul/x/upgrade/adminreg/facade/v0"
  7	implv0 "gno.land/r/moul/x/upgrade/adminreg/impl/v0"
  8
  9	"gno.land/p/nt/uassert/v0"
 10)
 11
 12// TestProposeThenAccept checks the two-step shape: deploying nominates, and
 13// only an owner transaction naming a path promotes it.
 14//
 15// Both impl realms are imported here, so both have already nominated themselves
 16// by the time this runs, and neither is serving.
 17func TestProposeThenAccept(cur realm, t *testing.T) {
 18	testing.SetRealm(testing.NewUserRealm(facade.Ownable.Owner()))
 19
 20	uassert.Equal(t, 2, len(facade.Candidates()), "both versions nominated themselves")
 21	uassert.Equal(t, "", facade.Live(), "nominating is not serving")
 22	uassert.AbortsContains(t, cur, "no implementation accepted", func() {
 23		facade.Greet("world")
 24	})
 25
 26	// Accepting names a path, not an object: this is a plain maketx call.
 27	facade.Accept(cross(cur), "gno.land/r/moul/x/upgrade/adminreg/impl/v0")
 28	uassert.Equal(t, "impl/v0", facade.Version())
 29	uassert.Equal(t, "hello, world", facade.Greet("world"))
 30
 31	// The upgrade, and the rollback, are the same single call.
 32	facade.Accept(cross(cur), "gno.land/r/moul/x/upgrade/adminreg/impl/v1")
 33	uassert.Equal(t, "HELLO, world!", facade.Greet("world"))
 34	facade.Accept(cross(cur), implv0.Path)
 35	uassert.Equal(t, "hello, world", facade.Greet("world"))
 36	facade.Accept(cross(cur), Path)
 37
 38	// A path nobody nominated cannot be accepted, however owned you are.
 39	uassert.AbortsContains(t, cur, "no candidate at", func() {
 40		facade.Accept(cross(cur), "gno.land/r/moul/x/upgrade/adminreg/impl/v9")
 41	})
 42
 43	// A non-owner cannot accept even a legitimate candidate.
 44	testing.SetRealm(testing.NewUserRealm("g1w4ek2u33ta047h6lta047h6lta047h6ldvdwpn"))
 45	uassert.AbortsContains(t, cur, "caller is not owner", func() {
 46		facade.Accept(cross(cur), implv0.Path)
 47	})
 48
 49	uassert.Equal(t, "adminreg/facade/v0 [open]\n"+
 50		"live: impl/v1 (gno.land/r/moul/x/upgrade/adminreg/impl/v1)\n"+
 51		"HELLO, world!\n"+
 52		"candidates: 2\n"+
 53		"- gno.land/r/moul/x/upgrade/adminreg/impl/v0\n"+
 54		"- gno.land/r/moul/x/upgrade/adminreg/impl/v1\n", facade.Render(""))
 55}
 56
 57// TestDispatchCost100 does 100 dispatches through this pattern's entry point,
 58// so `gno test -print-runtime-metrics` prints a cycle count for a known
 59// workload. Its twin is TestDispatchCost100 in schema/impl/gen1.
 60//
 61func TestDispatchCost100(cur realm, t *testing.T) {
 62	// Runs after TestProposeThenAccept, which leaves v1 live, and before
 63	// TestLadder, which freezes. Reads only, so it disturbs neither.
 64	for i := 0; i < 100; i++ {
 65		facade.Greet("world")
 66	}
 67	uassert.Equal(t, "HELLO, world!", facade.Greet("world"))
 68}
 69
 70// TestLadder walks the three rungs and checks the ratchet refuses to loosen.
 71//
 72// Runs after TestProposeThenAccept on purpose: every rung is one-way, so the
 73// tests that need to propose and accept freely must already have run.
 74func TestLadder(cur realm, t *testing.T) {
 75	testing.SetRealm(testing.NewUserRealm(facade.Ownable.Owner()))
 76	uassert.Equal(t, facade.StageOpen, facade.Stage())
 77
 78	// Rung 2: no new code, but rolling back among what exists still works.
 79	facade.Close(cross(cur))
 80	uassert.Equal(t, facade.StageClosed, facade.Stage())
 81	uassert.AbortsContains(t, cur, "no new candidate may be proposed", func() {
 82		Propose(cross(cur))
 83	})
 84	facade.Accept(cross(cur), implv0.Path)
 85	uassert.Equal(t, "hello, world", facade.Greet("world"), "rollback survives Close")
 86	facade.Accept(cross(cur), Path)
 87
 88	// The ratchet only turns one way, even for the owner.
 89	uassert.AbortsContains(t, cur, "only tightens", func() {
 90		facade.Close(cross(cur))
 91	})
 92
 93	// Rung 3: nothing is accepted again.
 94	facade.Freeze(cross(cur))
 95	uassert.Equal(t, facade.StageFrozen, facade.Stage())
 96	uassert.AbortsContains(t, cur, "is frozen", func() {
 97		facade.Accept(cross(cur), implv0.Path)
 98	})
 99	uassert.AbortsContains(t, cur, "only tightens", func() {
100		facade.Freeze(cross(cur))
101	})
102
103	// What was live stays live, and the render says which rung it is on.
104	uassert.Equal(t, "HELLO, world!", facade.Greet("world"))
105	uassert.Equal(t, "adminreg/facade/v0 [frozen]\n"+
106		"live: impl/v1 (gno.land/r/moul/x/upgrade/adminreg/impl/v1)\n"+
107		"HELLO, world!\n"+
108		"candidates: 2\n"+
109		"- gno.land/r/moul/x/upgrade/adminreg/impl/v0\n"+
110		"- gno.land/r/moul/x/upgrade/adminreg/impl/v1\n", facade.Render(""))
111}