|
| 1 | +package f3 |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/filecoin-project/go-f3/ec" |
| 8 | + "github.com/filecoin-project/go-f3/internal/clock" |
| 9 | + "github.com/filecoin-project/go-f3/manifest" |
| 10 | +) |
| 11 | + |
| 12 | +var _ ec.TipSet = (*tipset)(nil) |
| 13 | + |
| 14 | +func TestComputeBootstrapDelay(t *testing.T) { |
| 15 | + period := 30 * time.Second |
| 16 | + bootstrapEpoch := 1000 |
| 17 | + m := manifest.Manifest{ |
| 18 | + BootstrapEpoch: int64(bootstrapEpoch), |
| 19 | + EC: manifest.EcConfig{ |
| 20 | + Period: period, |
| 21 | + }, |
| 22 | + } |
| 23 | + |
| 24 | + clock := clock.NewMock() |
| 25 | + genesis := time.Date(2020, time.January, 12, 01, 01, 01, 00, time.UTC) |
| 26 | + |
| 27 | + tt := []struct { |
| 28 | + name string |
| 29 | + time time.Time |
| 30 | + ts tipset |
| 31 | + want time.Duration |
| 32 | + }{ |
| 33 | + { |
| 34 | + name: "in sync - right before bootstrap", |
| 35 | + time: genesis.Add(time.Duration(bootstrapEpoch-1) * period), |
| 36 | + ts: tipset{genesis: genesis, epoch: int64(bootstrapEpoch - 1), period: period}, |
| 37 | + want: period, |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "in sync - right at bootstrap", |
| 41 | + time: genesis.Add(time.Duration(bootstrapEpoch) * period), |
| 42 | + ts: tipset{genesis: genesis, epoch: int64(bootstrapEpoch), period: period}, |
| 43 | + want: 0, |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "in sync - right after bootstrap", |
| 47 | + time: genesis.Add(time.Duration(bootstrapEpoch+1) * period), |
| 48 | + ts: tipset{genesis: genesis, epoch: int64(bootstrapEpoch + 1), period: period}, |
| 49 | + want: 0, |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "in sync - right before bootstrap (offset)", |
| 53 | + time: genesis.Add(time.Duration(bootstrapEpoch-1)*period + 15*time.Second), |
| 54 | + ts: tipset{genesis: genesis, epoch: int64(bootstrapEpoch - 1), period: period}, |
| 55 | + want: 15 * time.Second, |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "in sync - right after bootstrap (offset)", |
| 59 | + time: genesis.Add(time.Duration(bootstrapEpoch)*period + 1*time.Second), |
| 60 | + ts: tipset{genesis: genesis, epoch: int64(bootstrapEpoch), period: period}, |
| 61 | + want: 0 * time.Second, |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "out of sync - way after bootstrap", |
| 65 | + time: genesis.Add(time.Duration(bootstrapEpoch+100)*period + 1*time.Second), |
| 66 | + ts: tipset{genesis: genesis, epoch: int64(bootstrapEpoch - 100), period: period}, |
| 67 | + want: 0 * time.Second, |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "out of sync - way before bootstrap", |
| 71 | + time: genesis.Add(time.Duration(bootstrapEpoch-30)*period + 1*time.Second), |
| 72 | + ts: tipset{genesis: genesis, epoch: int64(bootstrapEpoch - 100), period: period}, |
| 73 | + want: 30*period - 1*time.Second, |
| 74 | + }, |
| 75 | + } |
| 76 | + |
| 77 | + for _, tc := range tt { |
| 78 | + t.Run(tc.name, func(t *testing.T) { |
| 79 | + clock.Set(tc.time) |
| 80 | + got := computeBootstrapDelay(&tc.ts, clock, m) |
| 81 | + if got != tc.want { |
| 82 | + t.Errorf("computeBootstrapDelay(%s, %v, %v) = %v, want %v", &tc.ts, tc.time, period, got, tc.want) |
| 83 | + } |
| 84 | + }) |
| 85 | + } |
| 86 | +} |
0 commit comments