github.com/koko1123/flow-go-1@v0.29.6/state/protocol/blocktimer/blocktimer_test.go (about) 1 package blocktimer 2 3 import ( 4 "fmt" 5 "testing" 6 "time" 7 8 "github.com/stretchr/testify/require" 9 10 "github.com/koko1123/flow-go-1/state/protocol" 11 ) 12 13 // TestBlockTimestamp_Validate tests that validation accepts valid time and rejects invalid 14 func TestBlockTimestamp_Validate(t *testing.T) { 15 t.Parallel() 16 builder, err := NewBlockTimer(10*time.Millisecond, 1*time.Second) 17 require.NoError(t, err) 18 t.Run("parentTime + minInterval + 1", func(t *testing.T) { 19 parentTime := time.Now().UTC() 20 blockTime := parentTime.Add(builder.minInterval + time.Millisecond) 21 require.NoError(t, builder.Validate(parentTime, blockTime)) 22 }) 23 t.Run("parentTime + minInterval", func(t *testing.T) { 24 parentTime := time.Now().UTC() 25 blockTime := parentTime.Add(builder.minInterval) 26 require.NoError(t, builder.Validate(parentTime, blockTime)) 27 }) 28 t.Run("parentTime + minInterval - 1", func(t *testing.T) { 29 parentTime := time.Now().UTC() 30 blockTime := parentTime.Add(builder.minInterval - time.Millisecond) 31 err := builder.Validate(parentTime, blockTime) 32 require.Error(t, err) 33 require.True(t, protocol.IsInvalidBlockTimestampError(err)) 34 }) 35 t.Run("parentTime + maxInterval - 1", func(t *testing.T) { 36 parentTime := time.Now().UTC() 37 blockTime := parentTime.Add(builder.maxInterval - time.Millisecond) 38 require.NoError(t, builder.Validate(parentTime, blockTime)) 39 }) 40 t.Run("parentTime + maxInterval", func(t *testing.T) { 41 parentTime := time.Now().UTC() 42 blockTime := parentTime.Add(builder.maxInterval) 43 require.NoError(t, builder.Validate(parentTime, blockTime)) 44 }) 45 t.Run("parentTime + maxInterval + 1", func(t *testing.T) { 46 parentTime := time.Now().UTC() 47 blockTime := parentTime.Add(builder.maxInterval + time.Millisecond) 48 err := builder.Validate(parentTime, blockTime) 49 require.Error(t, err) 50 require.True(t, protocol.IsInvalidBlockTimestampError(err)) 51 }) 52 } 53 54 // TestBlockTimestamp_Build tests that builder correctly generates new block time 55 func TestBlockTimestamp_Build(t *testing.T) { 56 t.Parallel() 57 minInterval := 100 * time.Millisecond 58 maxInterval := 10 * time.Second 59 deltas := []time.Duration{0, minInterval, maxInterval} 60 61 // this test tries to cover next scenarious in generic way: 62 // now = parent - 1 63 // now = parent 64 // now = parent + 1 65 // now = parent + minInterval - 1 66 // now = parent + minInterval 67 // now = parent + minInterval + 1 68 // now = parent + maxInterval - 1 69 // now = parent + maxInterval 70 // now = parent + maxInterval + 1 71 for _, durationDelta := range deltas { 72 duration := durationDelta 73 t.Run(fmt.Sprintf("duration-delta-%d", durationDelta), func(t *testing.T) { 74 builder, err := NewBlockTimer(minInterval, maxInterval) 75 require.NoError(t, err) 76 77 parentTime := time.Now().UTC() 78 79 // now = parentTime + delta + {-1, 0, +1} 80 for i := -1; i <= 1; i++ { 81 builder.generator = func() time.Time { 82 return parentTime.Add(duration + time.Millisecond*time.Duration(i)) 83 } 84 85 blockTime := builder.Build(parentTime) 86 require.NoError(t, builder.Validate(parentTime, blockTime)) 87 } 88 }) 89 } 90 }