| new file mode 100644 |
| index 000000000..cfea17254 |
| --- /dev/null |
| +++ b/internal/lease/previousboot_test.go |
| @@ -0,0 +1,84 @@ |
| +package lease_test |
| + |
| +import ( |
| + "errors" |
| + "os" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/hafresh" |
| + "github.com/carlosframework/platform/internal/lease" |
| + leasetest "github.com/carlosframework/platform/internal/lease/locktest" |
| +) |
| + |
| +// #451. This box, booted as b2, meets an UNEXPIRED legacy lock at |
| +// generation 9. Only one its own earlier boot wrote is taken at once. |
| +func previousBootBox(t *testing.T) *leasetest.LockFixture { |
| + t.Helper() |
| + lease.ResetForTest() |
| + t.Cleanup(lease.ResetForTest) |
| + lease.SetSelf(hafresh.Self{Ref: "r", IID: "i-1", Boot: "b2"}) |
| + return leasetest.NewLockFixture(t) |
| +} |
| + |
| +func unexpired() time.Time { return time.Now().Add(10 * time.Minute) } |
| + |
| +func TestLegacyTakeTakesThePreviousBootsLock(t *testing.T) { |
| + fx := previousBootBox(t) |
| + fx.PutLock("instances/h", leaseBody(hafresh.OwnerFor(hafresh.Self{Ref: "r", IID: "i-1", Boot: "b1"}, 41, false), unexpired())) |
| + h, err := lease.Acquire(ctx, fx.Bucket, "instances/h", "") |
| + if err != nil { |
| + t.Fatalf("the previous boot's lock was not taken: %v", err) |
| + } |
| + defer h.Release(ctx) |
| + v, _ := hafresh.DecodeLock(fx.LockBody("instances/h"), "") |
| + if v.Lock.Generation != 10 { |
| + t.Fatalf("generation %d, want 10", v.Lock.Generation) |
| + } |
| + if got, want := v.Lock.Owner, hafresh.OwnerFor(lease.CurrentSelf(), os.Getpid(), false); got != want { |
| + t.Fatalf("owner %q, want %q", got, want) |
| + } |
| +} |
| + |
| +func TestLegacyTakeRefusesTheSameBoot(t *testing.T) { |
| + refusesHeld(t, hafresh.OwnerFor(hafresh.Self{Ref: "r", IID: "i-1", Boot: "b2"}, 41, false)) |
| +} |
| + |
| +func TestLegacyTakeRefusesAnotherInstance(t *testing.T) { |
| + refusesHeld(t, hafresh.OwnerFor(hafresh.Self{Ref: "r", IID: "i-2", Boot: "b1"}, 41, false)) |
| +} |
| + |
| +func TestLegacyTakeRefusesAnOldOwner(t *testing.T) { |
| + refusesHeld(t, "box/v1;ref=r;iid=i-1;pid=41") |
| +} |
| + |
| +func TestLegacyTakeStillRefusesHALocks(t *testing.T) { |
| + fx := previousBootBox(t) |
| + body := leaseBody(hafresh.OwnerFor(hafresh.Self{Ref: "r", IID: "i-1", Boot: "b1"}, 41, true), unexpired()) |
| + fx.PutLock("instances/h", body) |
| + _, err := lease.Acquire(ctx, fx.Bucket, "instances/h", "") |
| + var ha *lease.HALockError |
| + if !errors.As(err, &ha) { |
| + t.Fatalf("an HA lock: %v", err) |
| + } |
| + if string(fx.LockBody("instances/h")) != string(body) { |
| + t.Fatal("the HA lock was written") |
| + } |
| +} |
| + |
| +// refusesHeld puts an unexpired lock owned by owner and checks the |
| +// acquire is refused as held, with the lock untouched. |
| +func refusesHeld(t *testing.T, owner string) { |
| + t.Helper() |
| + fx := previousBootBox(t) |
| + body := leaseBody(owner, unexpired()) |
| + fx.PutLock("instances/h", body) |
| + _, err := lease.Acquire(ctx, fx.Bucket, "instances/h", "") |
| + var held *lease.HeldError |
| + if !errors.As(err, &held) || held.Owner != owner { |
| + t.Fatalf("owner %q: %v", owner, err) |
| + } |
| + if string(fx.LockBody("instances/h")) != string(body) { |
| + t.Fatalf("owner %q: the lock was written", owner) |
| + } |
| +} |