func TestRemoveLeaseIfExpiring(t *testing.T) { defer leaktest.AfterTest(t)() p := planner{session: &Session{context: context.Background()}} mc := hlc.NewManualClock(123) p.leaseMgr = &LeaseManager{LeaseStore: LeaseStore{clock: hlc.NewClock(mc.UnixNano, time.Nanosecond)}} p.leases = make([]*LeaseState, 0) txn := client.Txn{Context: context.Background()} p.setTxn(&txn) if p.removeLeaseIfExpiring(nil) { t.Error("expected false with nil input") } // Add a lease to the planner. d := int64(LeaseDuration) l1 := &LeaseState{expiration: parser.DTimestamp{Time: time.Unix(0, mc.UnixNano()+d+1)}} p.leases = append(p.leases, l1) et := hlc.Timestamp{WallTime: l1.Expiration().UnixNano()} txn.UpdateDeadlineMaybe(et) if p.removeLeaseIfExpiring(l1) { t.Error("expected false with a non-expiring lease") } if !p.txn.GetDeadline().Equal(et) { t.Errorf("expected deadline %s but got %s", et, p.txn.GetDeadline()) } // Advance the clock so that l1 will be expired. mc.Increment(d + 1) // Add another lease. l2 := &LeaseState{expiration: parser.DTimestamp{Time: time.Unix(0, mc.UnixNano()+d+1)}} p.leases = append(p.leases, l2) if !p.removeLeaseIfExpiring(l1) { t.Error("expected true with an expiring lease") } et = hlc.Timestamp{WallTime: l2.Expiration().UnixNano()} txn.UpdateDeadlineMaybe(et) if !(len(p.leases) == 1 && p.leases[0] == l2) { t.Errorf("expected leases to contain %s but has %s", l2, p.leases) } if !p.txn.GetDeadline().Equal(et) { t.Errorf("expected deadline %s, but got %s", et, p.txn.GetDeadline()) } }
// setTxnTimestamps sets the transaction's proto timestamps and deadline // to ts. This is for use with AS OF queries, and should be called in the // retry block (except in the case of prepare which doesn't use retry). The // deadline-checking code checks that the `Timestamp` field of the proto // hasn't exceeded the deadline. Since we set the Timestamp field each retry, // it won't ever exceed the deadline, and thus setting the deadline here is // not strictly needed. However, it doesn't do anything incorrect and it will // possibly find problems if things change in the future, so it is left in. func setTxnTimestamps(txn *client.Txn, ts hlc.Timestamp) { txn.Proto.Timestamp = ts txn.Proto.OrigTimestamp = ts txn.Proto.MaxTimestamp = ts txn.UpdateDeadlineMaybe(ts) }