2018-04-16 09:26:15 +00:00
|
|
|
package badger
|
|
|
|
|
|
|
|
import (
|
2022-02-03 09:03:37 +00:00
|
|
|
"sync"
|
2018-04-16 09:26:15 +00:00
|
|
|
"testing"
|
|
|
|
|
2021-07-16 04:10:34 +00:00
|
|
|
"github.com/appleboy/gorush/config"
|
2022-11-09 06:05:09 +00:00
|
|
|
"github.com/appleboy/gorush/core"
|
|
|
|
|
2018-04-16 09:26:15 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBadgerEngine(t *testing.T) {
|
|
|
|
var val int64
|
|
|
|
|
2021-07-16 04:10:34 +00:00
|
|
|
cfg, _ := config.LoadConf()
|
2018-04-16 09:26:15 +00:00
|
|
|
|
2021-07-16 04:10:34 +00:00
|
|
|
badger := New(cfg)
|
2018-04-16 09:26:15 +00:00
|
|
|
err := badger.Init()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2022-11-09 06:05:09 +00:00
|
|
|
badger.Add(core.HuaweiSuccessKey, 10)
|
|
|
|
val = badger.Get(core.HuaweiSuccessKey)
|
2018-04-16 09:26:15 +00:00
|
|
|
assert.Equal(t, int64(10), val)
|
2022-11-09 06:05:09 +00:00
|
|
|
badger.Add(core.HuaweiSuccessKey, 10)
|
|
|
|
val = badger.Get(core.HuaweiSuccessKey)
|
2018-04-16 09:26:15 +00:00
|
|
|
assert.Equal(t, int64(20), val)
|
|
|
|
|
2022-11-09 06:05:09 +00:00
|
|
|
badger.Set(core.HuaweiSuccessKey, 0)
|
|
|
|
val = badger.Get(core.HuaweiSuccessKey)
|
2018-04-16 09:26:15 +00:00
|
|
|
assert.Equal(t, int64(0), val)
|
2020-04-23 07:39:24 +00:00
|
|
|
|
2022-02-03 09:03:37 +00:00
|
|
|
// test concurrency issues
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
2022-11-09 06:05:09 +00:00
|
|
|
badger.Add(core.HuaweiSuccessKey, 1)
|
2022-02-03 09:03:37 +00:00
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
wg.Wait()
|
2022-11-09 06:05:09 +00:00
|
|
|
val = badger.Get(core.HuaweiSuccessKey)
|
2022-02-03 09:03:37 +00:00
|
|
|
assert.Equal(t, int64(10), val)
|
|
|
|
|
2020-04-23 07:39:24 +00:00
|
|
|
assert.NoError(t, badger.Close())
|
2018-04-16 09:26:15 +00:00
|
|
|
}
|