func TestGenerateRandomInt64InRange(t *testing.T) { v2testing.Current(t) base := time.Now().Unix() delta := 100 for i := 0; i < 100; i++ { v := GenerateRandomInt64InRange(base, delta) assert.Int64(v).AtMost(base + int64(delta)) assert.Int64(v).AtLeast(base - int64(delta)) } }
func TestGenerateRandomInt64InRange(t *testing.T) { assert := assert.On(t) base := time.Now().Unix() delta := 100 generator := NewTimestampGenerator(Timestamp(base), delta) for i := 0; i < 100; i++ { v := int64(generator()) assert.Int64(v).AtMost(base + int64(delta)) assert.Int64(v).AtLeast(base - int64(delta)) } }
func TestSwitchAccount(t *testing.T) { v2testing.Current(t) sa := &SwitchAccount{ Port: 1234, ID: uuid.New(), AlterIds: 1024, ValidUntil: time.Now(), } cmd, err := CreateResponseCommand(1) assert.Error(err).IsNil() buffer := bytes.NewBuffer(make([]byte, 0, 1024)) nBytes, err := sa.Marshal(buffer) assert.Error(err).IsNil() assert.Int(nBytes).Equals(buffer.Len()) cmd.Unmarshal(buffer.Bytes()) sa2, ok := cmd.(*SwitchAccount) assert.Bool(ok).IsTrue() netassert.Port(sa.Port).Equals(sa2.Port) assert.String(sa.ID).Equals(sa2.ID.String()) assert.Uint16(sa.AlterIds.Value()).Equals(sa2.AlterIds.Value()) assert.Int64(sa.ValidUntil.Unix()).Equals(sa2.ValidUntil.Unix()) }
func TestNoRetry(t *testing.T) { v2testing.Current(t) startTime := time.Now().Unix() err := Timed(10, 100000).On(func() error { return nil }) endTime := time.Now().Unix() assert.Error(err).IsNil() assert.Int64(endTime - startTime).AtLeast(0) }
func TestRetryExhausted(t *testing.T) { v2testing.Current(t) startTime := time.Now() called := 0 err := Timed(2, 1000).On(func() error { if called < 5 { called++ return errorTestOnly } return nil }) duration := time.Since(startTime) assert.Error(err).Equals(errorRetryFailed) assert.Int64(int64(duration / time.Millisecond)).AtLeast(1900) }
func TestRetryMultiple(t *testing.T) { v2testing.Current(t) startTime := time.Now() called := 0 err := Timed(10, 1000).On(func() error { if called < 5 { called++ return errorTestOnly } return nil }) duration := time.Since(startTime) assert.Error(err).IsNil() assert.Int64(int64(duration / time.Millisecond)).AtLeast(4900) }