You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
41 lines
1.1 KiB
41 lines
1.1 KiB
package main
|
|
|
|
import (
|
|
"context"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"tera-clone/services/tera-api/internal/domain"
|
|
)
|
|
|
|
type pressedResetButton struct{}
|
|
|
|
func (pressedResetButton) ResetButtonPressed(context.Context) (bool, error) { return true, nil }
|
|
|
|
type countingFactoryResetStore struct{ calls atomic.Int32 }
|
|
|
|
func (s *countingFactoryResetStore) FactoryReset(context.Context) (domain.Operation, error) {
|
|
s.calls.Add(1)
|
|
return domain.Operation{OperationID: "factory-reset-test"}, nil
|
|
}
|
|
|
|
func TestFactoryResetButtonMonitorTriggersOnceWhileHeld(t *testing.T) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
store := &countingFactoryResetStore{}
|
|
done := make(chan struct{})
|
|
go func() {
|
|
defer close(done)
|
|
runFactoryResetButtonMonitor(ctx, pressedResetButton{}, store, 2*time.Millisecond, 5*time.Millisecond, nil, func(err error) { t.Error(err) })
|
|
}()
|
|
deadline := time.Now().Add(250 * time.Millisecond)
|
|
for store.calls.Load() == 0 && time.Now().Before(deadline) {
|
|
time.Sleep(time.Millisecond)
|
|
}
|
|
time.Sleep(20 * time.Millisecond)
|
|
cancel()
|
|
<-done
|
|
if store.calls.Load() != 1 {
|
|
t.Fatalf("factory reset calls = %d, want 1", store.calls.Load())
|
|
}
|
|
}
|
|
|