components-contrib/tests/certification/flow/simulate/simulate.go

42 lines
1.2 KiB
Go

/*
Copyright 2021 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package simulate
import (
"errors"
"sync/atomic"
"github.com/dapr/components-contrib/tests/certification/flow"
)
func PeriodicError(ctx flow.Context, frequency uint64) func() error {
counter := uint64(0)
errorCount := uint64(0)
return func() error {
next := atomic.AddUint64(&counter, 1)
// This behavior is standard to repro a failure of one message in a batch.
if atomic.LoadUint64(&errorCount) < 2 || next%frequency == 0 {
// First message errors just to give time for more messages to pile up.
// Second error is to force an error in a batch.
ec := atomic.AddUint64(&errorCount, 1)
ctx.Logf("Simulating error %d", ec)
return errors.New("simulated error")
}
return nil
}
}