bbgo: refactor isolation and add more tests

This commit is contained in:
c9s 2022-10-04 17:23:43 +08:00
parent 731e5569d0
commit a8d9911e36
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
4 changed files with 35 additions and 9 deletions

View File

@ -30,14 +30,14 @@ func (g *GracefulShutdown) Shutdown(ctx context.Context) {
}
func OnShutdown(ctx context.Context, f ShutdownHandler) {
isolatedContext := NewIsolationFromContext(ctx)
isolatedContext := GetIsolationFromContext(ctx)
isolatedContext.gracefulShutdown.OnShutdown(f)
}
func Shutdown(ctx context.Context) {
logrus.Infof("shutting down...")
isolatedContext := NewIsolationFromContext(ctx)
isolatedContext := GetIsolationFromContext(ctx)
todo := context.WithValue(context.TODO(), IsolationContextKey, isolatedContext)
timeoutCtx, cancel := context.WithTimeout(todo, 30*time.Second)

View File

@ -8,21 +8,21 @@ import (
const IsolationContextKey = "bbgo"
var defaultIsolation = NewIsolation()
var defaultIsolation = NewDefaultIsolation()
type Isolation struct {
gracefulShutdown GracefulShutdown
persistenceServiceFacade *service.PersistenceServiceFacade
}
func NewIsolation() *Isolation {
func NewDefaultIsolation() *Isolation {
return &Isolation{
gracefulShutdown: GracefulShutdown{},
persistenceServiceFacade: DefaultPersistenceServiceFacade,
persistenceServiceFacade: persistenceServiceFacade,
}
}
func NewIsolationFromContext(ctx context.Context) *Isolation {
func GetIsolationFromContext(ctx context.Context) *Isolation {
isolatedContext, ok := ctx.Value(IsolationContextKey).(*Isolation)
if ok {
return isolatedContext

View File

@ -0,0 +1,24 @@
package bbgo
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
)
func TestGetIsolationFromContext(t *testing.T) {
ctx := context.Background()
isolation := GetIsolationFromContext(ctx)
assert.NotNil(t, isolation)
assert.NotNil(t, isolation.persistenceServiceFacade)
assert.NotNil(t, isolation.gracefulShutdown)
}
func TestNewDefaultIsolation(t *testing.T) {
isolation := NewDefaultIsolation()
assert.NotNil(t, isolation)
assert.NotNil(t, isolation.persistenceServiceFacade)
assert.NotNil(t, isolation.gracefulShutdown)
assert.Equal(t, persistenceServiceFacade, isolation.persistenceServiceFacade)
}

View File

@ -10,11 +10,11 @@ import (
"github.com/c9s/bbgo/pkg/service"
)
var DefaultPersistenceServiceFacade = &service.PersistenceServiceFacade{
var defaultPersistenceServiceFacade = &service.PersistenceServiceFacade{
Memory: service.NewMemoryService(),
}
var persistenceServiceFacade = DefaultPersistenceServiceFacade
var persistenceServiceFacade = defaultPersistenceServiceFacade
// Sync syncs the object properties into the persistence layer
func Sync(ctx context.Context, obj interface{}) {
@ -24,7 +24,9 @@ func Sync(ctx context.Context, obj interface{}) {
return
}
ps := persistenceServiceFacade.Get()
isolation := GetIsolationFromContext(ctx)
ps := isolation.persistenceServiceFacade.Get()
err := storePersistenceFields(obj, id, ps)
if err != nil {
log.WithError(err).Errorf("persistence sync failed")