2018-10-10 17:42:31 +00:00
|
|
|
package mock
|
|
|
|
|
|
|
|
import (
|
2019-02-25 16:11:20 +00:00
|
|
|
"context"
|
2018-10-10 17:42:31 +00:00
|
|
|
"sync"
|
|
|
|
|
2019-01-08 00:37:16 +00:00
|
|
|
"github.com/influxdata/influxdb/models"
|
2018-10-10 17:42:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// PointsWriter is a mock structure for writing points.
|
|
|
|
type PointsWriter struct {
|
2019-08-03 04:52:33 +00:00
|
|
|
timesWriteCalled int
|
|
|
|
mu sync.RWMutex
|
|
|
|
Points []models.Point
|
|
|
|
Err error
|
2018-10-10 17:42:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ForceError is for error testing, if WritePoints is called after ForceError, it will return that error.
|
|
|
|
func (p *PointsWriter) ForceError(err error) {
|
|
|
|
p.mu.Lock()
|
|
|
|
p.Err = err
|
|
|
|
p.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// WritePoints writes points to the PointsWriter that will be exposed in the Values.
|
2019-02-25 16:11:20 +00:00
|
|
|
func (p *PointsWriter) WritePoints(ctx context.Context, points []models.Point) error {
|
2018-10-10 17:42:31 +00:00
|
|
|
p.mu.Lock()
|
2019-08-03 04:52:33 +00:00
|
|
|
p.timesWriteCalled++
|
2018-10-10 17:42:31 +00:00
|
|
|
p.Points = append(p.Points, points...)
|
|
|
|
err := p.Err
|
|
|
|
p.mu.Unlock()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next returns the next (oldest) batch of values.
|
|
|
|
func (p *PointsWriter) Next() models.Point {
|
|
|
|
var points models.Point
|
|
|
|
p.mu.RLock()
|
|
|
|
if len(p.Points) == 0 {
|
|
|
|
p.mu.RUnlock()
|
|
|
|
return points
|
|
|
|
}
|
|
|
|
p.mu.RUnlock()
|
|
|
|
|
|
|
|
p.mu.Lock()
|
|
|
|
defer p.mu.Unlock()
|
|
|
|
points, p.Points = p.Points[0], p.Points[1:]
|
|
|
|
return points
|
|
|
|
}
|
2019-08-03 04:52:33 +00:00
|
|
|
|
|
|
|
func (p *PointsWriter) WritePointsCalled() int {
|
|
|
|
p.mu.Lock()
|
|
|
|
defer p.mu.Unlock()
|
|
|
|
return p.timesWriteCalled
|
|
|
|
}
|