2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2022-12-16 21:49:26 +00:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
2023-01-26 20:20:37 +00:00
|
|
|
"time"
|
2022-12-16 21:49:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type testReconciler struct {
|
|
|
|
received chan Request
|
|
|
|
response error
|
|
|
|
mutex sync.Mutex
|
|
|
|
stepChan chan struct{}
|
|
|
|
stopChan chan struct{}
|
|
|
|
ctx context.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *testReconciler) Reconcile(ctx context.Context, req Request) error {
|
|
|
|
if r.stepChan != nil {
|
|
|
|
select {
|
|
|
|
case <-r.stopChan:
|
|
|
|
return nil
|
|
|
|
case <-r.stepChan:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-r.stopChan:
|
|
|
|
return nil
|
|
|
|
case r.received <- req:
|
|
|
|
}
|
|
|
|
|
|
|
|
r.mutex.Lock()
|
|
|
|
defer r.mutex.Unlock()
|
|
|
|
return r.response
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *testReconciler) setResponse(err error) {
|
|
|
|
r.mutex.Lock()
|
|
|
|
defer r.mutex.Unlock()
|
|
|
|
r.response = err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *testReconciler) step() {
|
|
|
|
r.stepChan <- struct{}{}
|
|
|
|
}
|
2023-01-26 20:20:37 +00:00
|
|
|
func (r *testReconciler) stepFor(duration time.Duration) {
|
|
|
|
select {
|
|
|
|
case r.stepChan <- struct{}{}:
|
|
|
|
case <-time.After(duration):
|
|
|
|
}
|
|
|
|
}
|
2022-12-16 21:49:26 +00:00
|
|
|
|
|
|
|
func (r *testReconciler) stop() {
|
|
|
|
close(r.stopChan)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTestReconciler(stepping bool) *testReconciler {
|
|
|
|
r := &testReconciler{
|
|
|
|
received: make(chan Request, 1000),
|
|
|
|
stopChan: make(chan struct{}),
|
|
|
|
}
|
|
|
|
if stepping {
|
|
|
|
r.stepChan = make(chan struct{})
|
|
|
|
}
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|