mirror of
https://github.com/tiennm99/keepalive.git
synced 2026-10-15 08:17:17 +00:00
fix(runtime): initialize resources per service
This commit is contained in:
1 parent
16cf6277e1
commit
6b0c2f5a4c
15 files changed
+501
-72
No files matched your search
@@ -0,0 +1,81 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/tiennm99/keepalive/adapter"
|
||||
)
|
||||
|
||||
type retryConnectAdapter struct {
|
||||
connects *atomic.Int32
|
||||
connected chan<- struct{}
|
||||
once *sync.Once
|
||||
}
|
||||
|
||||
func (a *retryConnectAdapter) Connect(context.Context) error {
|
||||
if a.connects.Add(1) == 1 {
|
||||
return errors.New("connect failed")
|
||||
}
|
||||
a.once.Do(func() { close(a.connected) })
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *retryConnectAdapter) Increment(context.Context) (int64, error) {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
func (a *retryConnectAdapter) Close(context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestRunServiceRetriesConnectFailure(t *testing.T) {
|
||||
oldReconnectDelay := reconnectDelay
|
||||
reconnectDelay = 10 * time.Millisecond
|
||||
defer func() { reconnectDelay = oldReconnectDelay }()
|
||||
|
||||
var connects atomic.Int32
|
||||
connected := make(chan struct{})
|
||||
var once sync.Once
|
||||
adapter.Registry["retry-test"] = func(adapter.Config) (adapter.Adapter, error) {
|
||||
return &retryConnectAdapter{connects: &connects, connected: connected, once: &once}, nil
|
||||
}
|
||||
defer delete(adapter.Registry, "retry-test")
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
var wg sync.WaitGroup
|
||||
runService(ctx, &wg, serviceConfig{
|
||||
Name: "retry-test",
|
||||
AdapterType: "retry-test",
|
||||
Interval: time.Hour,
|
||||
Config: adapter.Config{},
|
||||
})
|
||||
|
||||
select {
|
||||
case <-connected:
|
||||
case <-time.After(time.Second):
|
||||
cancel()
|
||||
t.Fatal("service did not retry and connect")
|
||||
}
|
||||
|
||||
cancel()
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
wg.Wait()
|
||||
close(done)
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-done:
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("service did not stop after context cancellation")
|
||||
}
|
||||
|
||||
if got := connects.Load(); got != 2 {
|
||||
t.Fatalf("connect attempts = %d, want 2", got)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user