2022-05-05 04:53:48 +00:00
|
|
|
package bbgo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
|
|
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
|
|
|
"github.com/c9s/bbgo/pkg/service"
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TestStruct struct {
|
2022-06-02 17:57:39 +00:00
|
|
|
*Environment
|
|
|
|
*Graceful
|
|
|
|
|
2022-05-05 04:53:48 +00:00
|
|
|
Position *types.Position `persistence:"position"`
|
|
|
|
Integer int64 `persistence:"integer"`
|
|
|
|
Integer2 int64 `persistence:"integer2"`
|
2022-05-05 05:16:46 +00:00
|
|
|
Float int64 `persistence:"float"`
|
|
|
|
String string `persistence:"string"`
|
2022-05-05 04:53:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TestStruct) InstanceID() string {
|
|
|
|
return "test-struct"
|
|
|
|
}
|
|
|
|
|
|
|
|
func preparePersistentServices() []service.PersistenceService {
|
|
|
|
mem := service.NewMemoryService()
|
|
|
|
jsonDir := &service.JsonPersistenceService{Directory: "testoutput/persistence"}
|
|
|
|
pss := []service.PersistenceService{
|
|
|
|
mem,
|
|
|
|
jsonDir,
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := os.LookupEnv("TEST_REDIS"); ok {
|
|
|
|
redisP := service.NewRedisPersistenceService(&service.RedisPersistenceConfig{
|
|
|
|
Host: "localhost",
|
|
|
|
Port: "6379",
|
|
|
|
DB: 0,
|
|
|
|
})
|
|
|
|
pss = append(pss, redisP)
|
|
|
|
}
|
|
|
|
|
|
|
|
return pss
|
|
|
|
}
|
|
|
|
|
2022-05-05 05:05:01 +00:00
|
|
|
func Test_callID(t *testing.T) {
|
|
|
|
id := callID(&TestStruct{})
|
|
|
|
assert.NotEmpty(t, id)
|
|
|
|
}
|
|
|
|
|
2022-06-02 17:57:39 +00:00
|
|
|
func Test_loadPersistenceFields(t *testing.T) {
|
|
|
|
var pss = preparePersistentServices()
|
|
|
|
|
|
|
|
for _, ps := range pss {
|
|
|
|
psName := reflect.TypeOf(ps).Elem().String()
|
|
|
|
t.Run(psName+"/empty", func(t *testing.T) {
|
|
|
|
b := &TestStruct{}
|
|
|
|
err := loadPersistenceFields(b, "test-empty", ps)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run(psName+"/nil", func(t *testing.T) {
|
|
|
|
var b *TestStruct = nil
|
|
|
|
err := loadPersistenceFields(b, "test-nil", ps)
|
|
|
|
assert.Equal(t, errCanNotIterateNilPointer, err)
|
|
|
|
})
|
2022-06-02 18:49:16 +00:00
|
|
|
|
|
|
|
t.Run(psName+"/pointer-field", func(t *testing.T) {
|
|
|
|
var a = &TestStruct{
|
|
|
|
Position: types.NewPosition("BTCUSDT", "BTC", "USDT"),
|
|
|
|
}
|
|
|
|
a.Position.Base = fixedpoint.NewFromFloat(10.0)
|
|
|
|
a.Position.AverageCost = fixedpoint.NewFromFloat(3343.0)
|
|
|
|
err := storePersistenceFields(a, "pointer-field-test", ps)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
b := &TestStruct{}
|
|
|
|
err = loadPersistenceFields(b, "pointer-field-test", ps)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, "10", a.Position.Base.String())
|
|
|
|
assert.Equal(t, "3343", a.Position.AverageCost.String())
|
|
|
|
})
|
2022-06-02 17:57:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-05 04:53:48 +00:00
|
|
|
func Test_storePersistenceFields(t *testing.T) {
|
|
|
|
var pss = preparePersistentServices()
|
|
|
|
|
|
|
|
var a = &TestStruct{
|
|
|
|
Integer: 1,
|
|
|
|
Integer2: 2,
|
2022-05-05 05:16:46 +00:00
|
|
|
Float: 3.0,
|
|
|
|
String: "foobar",
|
2022-05-05 04:53:48 +00:00
|
|
|
Position: types.NewPosition("BTCUSDT", "BTC", "USDT"),
|
|
|
|
}
|
|
|
|
|
|
|
|
a.Position.Base = fixedpoint.NewFromFloat(10.0)
|
|
|
|
a.Position.AverageCost = fixedpoint.NewFromFloat(3343.0)
|
|
|
|
|
|
|
|
for _, ps := range pss {
|
2022-06-02 17:57:39 +00:00
|
|
|
psName := reflect.TypeOf(ps).Elem().String()
|
|
|
|
t.Run("all/"+psName, func(t *testing.T) {
|
2022-05-05 05:05:01 +00:00
|
|
|
id := callID(a)
|
|
|
|
err := storePersistenceFields(a, id, ps)
|
2022-05-05 04:53:48 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
var i int64
|
2022-05-05 09:56:41 +00:00
|
|
|
store := ps.NewStore("state", "test-struct", "integer")
|
2022-05-05 04:53:48 +00:00
|
|
|
err = store.Load(&i)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(1), i)
|
|
|
|
|
|
|
|
var p *types.Position
|
2022-05-05 09:56:41 +00:00
|
|
|
store = ps.NewStore("state", "test-struct", "position")
|
2022-05-05 04:53:48 +00:00
|
|
|
err = store.Load(&p)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, fixedpoint.NewFromFloat(10.0), p.Base)
|
|
|
|
assert.Equal(t, fixedpoint.NewFromFloat(3343.0), p.AverageCost)
|
|
|
|
|
|
|
|
var b = &TestStruct{}
|
2022-05-05 05:05:01 +00:00
|
|
|
err = loadPersistenceFields(b, id, ps)
|
2022-05-05 04:53:48 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, a.Integer, b.Integer)
|
|
|
|
assert.Equal(t, a.Integer2, b.Integer2)
|
2022-05-05 05:16:46 +00:00
|
|
|
assert.Equal(t, a.Float, b.Float)
|
|
|
|
assert.Equal(t, a.String, b.String)
|
2022-05-05 04:53:48 +00:00
|
|
|
assert.Equal(t, a.Position, b.Position)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|