github.com/decred/dcrlnd@v0.7.6/channeldb/dcrmigrations/migration02/dcrmigration02_test.go (about) 1 package dcrmigration02 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/decred/dcrlnd/channeldb/migtest" 8 "github.com/decred/dcrlnd/kvdb" 9 ) 10 11 var ( 12 hexStr = migtest.Hex 13 mockNow = time.Unix(1711465236, 0) 14 wantNewTs = mockNow.Add(-pastOffsetFix) 15 16 pub01 = hexStr("032195e05c87bf05b4fe34d7f2dd629390460e44fceab1f48252878d79dafa5779") 17 pub02 = hexStr("027e8c63c6eb86cd3bf64f58d6a86539bab58a1c25a9bc8c9e01e0a938e7b19821") 18 pub03 = hexStr("020ed7807b8e384df0cdaa3ef89f71e0ffc68a67df69c7674d24fe2896ff7ee8da") 19 20 pastTime = string(byteOrder.AppendUint64(nil, uint64(time.Unix(1518112800, 0).UnixNano()))) 21 futureTime = string(byteOrder.AppendUint64(nil, uint64(mockNow.Add(time.Second).UnixNano()))) 22 futureTimeAfterMig = string(byteOrder.AppendUint64(nil, uint64(wantNewTs.UnixNano()))) 23 peersPreMigration = map[string]interface{}{ 24 pub01: map[string]interface{}{ 25 string(peerLastGossipMsgTSKey): pastTime, 26 }, 27 pub02: map[string]interface{}{ 28 string(peerLastGossipMsgTSKey): futureTime, 29 }, 30 pub03: map[string]interface{}{}, // Peer without timestamp 31 } 32 33 peersPostMigration = map[string]interface{}{ 34 pub01: map[string]interface{}{ 35 string(peerLastGossipMsgTSKey): pastTime, 36 }, 37 pub02: map[string]interface{}{ 38 string(peerLastGossipMsgTSKey): futureTimeAfterMig, 39 }, 40 pub03: map[string]interface{}{}, // Peer without timestamp 41 } 42 ) 43 44 func TestRemoveFutureTimestamps(t *testing.T) { 45 // Modify now() to return a mock date. 46 nowFunc = func() time.Time { return mockNow } 47 48 // Prime the database with peer data in the past and future. 49 before := func(tx kvdb.RwTx) error { 50 return migtest.RestoreDB(tx, peersBucket, peersPreMigration) 51 } 52 53 // Double check the future dates were migrated to the past. 54 after := func(tx kvdb.RwTx) error { 55 return migtest.VerifyDB(tx, peersBucket, peersPostMigration) 56 } 57 58 migtest.ApplyMigration(t, before, after, RemoveFutureTimestampFromPeers, false) 59 }