2016-05-02 03:04:19 +00:00
|
|
|
package memory
|
|
|
|
|
|
|
|
import (
|
2022-02-03 09:03:37 +00:00
|
|
|
"sync"
|
2016-05-02 03:04:19 +00:00
|
|
|
"testing"
|
2017-01-19 09:08:12 +00:00
|
|
|
|
2022-11-09 06:05:09 +00:00
|
|
|
"github.com/appleboy/gorush/core"
|
2022-02-03 09:03:37 +00:00
|
|
|
|
2017-01-19 09:08:12 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2016-05-02 03:04:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMemoryEngine(t *testing.T) {
|
|
|
|
var val int64
|
|
|
|
|
2016-05-02 09:03:08 +00:00
|
|
|
memory := New()
|
2022-02-03 09:03:37 +00:00
|
|
|
err := memory.Init()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2022-11-09 06:05:09 +00:00
|
|
|
memory.Add(core.HuaweiSuccessKey, 10)
|
|
|
|
val = memory.Get(core.HuaweiSuccessKey)
|
2022-02-03 09:03:37 +00:00
|
|
|
assert.Equal(t, int64(10), val)
|
2022-11-09 06:05:09 +00:00
|
|
|
memory.Add(core.HuaweiSuccessKey, 10)
|
|
|
|
val = memory.Get(core.HuaweiSuccessKey)
|
2022-02-03 09:03:37 +00:00
|
|
|
assert.Equal(t, int64(20), val)
|
|
|
|
|
2022-11-09 06:05:09 +00:00
|
|
|
memory.Set(core.HuaweiSuccessKey, 0)
|
|
|
|
val = memory.Get(core.HuaweiSuccessKey)
|
2016-08-03 06:30:21 +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
|
|
|
memory.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 = memory.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, memory.Close())
|
2016-05-02 03:04:19 +00:00
|
|
|
}
|