// TestClock performs a complete test of all basic phenomena, // including backward jumps in local physical time and clock offset. func TestClock(t *testing.T) { m := NewManualClock(0) c := NewClock(m.UnixNano) c.SetMaxOffset(1000) expectedHistory := []struct { // The physical time that this event should take place at. wallClock int64 event Event // If this is a receive event, this holds the "input" timestamp. input *roachpb.Timestamp // The expected timestamp generated from the input. expected roachpb.Timestamp }{ // A few valid steps to warm up. {5, SEND, nil, roachpb.Timestamp{WallTime: 5, Logical: 0}}, {6, SEND, nil, roachpb.Timestamp{WallTime: 6, Logical: 0}}, {10, RECV, &roachpb.Timestamp{WallTime: 10, Logical: 5}, roachpb.Timestamp{WallTime: 10, Logical: 6}}, // Our clock mysteriously jumps back. {7, SEND, nil, roachpb.Timestamp{WallTime: 10, Logical: 7}}, // Wall clocks coincide, but the local logical clock wins. {8, RECV, &roachpb.Timestamp{WallTime: 10, Logical: 4}, roachpb.Timestamp{WallTime: 10, Logical: 8}}, // Wall clocks coincide, but the remote logical clock wins. {10, RECV, &roachpb.Timestamp{WallTime: 10, Logical: 99}, roachpb.Timestamp{WallTime: 10, Logical: 100}}, // The physical clock has caught up and takes over. {11, RECV, &roachpb.Timestamp{WallTime: 10, Logical: 31}, roachpb.Timestamp{WallTime: 11, Logical: 0}}, {11, SEND, nil, roachpb.Timestamp{WallTime: 11, Logical: 1}}, } var current roachpb.Timestamp for i, step := range expectedHistory { m.Set(step.wallClock) switch step.event { case SEND: current = c.Now() case RECV: fallthrough default: previous := c.Timestamp() current = c.Update(*step.input) if current.Equal(previous) { t.Errorf("%d: clock not updated", i) } } if !current.Equal(step.expected) { t.Fatalf("HLC error: %d expected %v, got %v", i, step.expected, current) } } c.Now() }