Skip to content

Commit

Permalink
Move mock files
Browse files Browse the repository at this point in the history
  • Loading branch information
KKKZOZ committed Jan 16, 2024
1 parent 7842625 commit 7e283a0
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 34 deletions.
4 changes: 0 additions & 4 deletions .github/workflows.yaml

This file was deleted.

33 changes: 33 additions & 0 deletions .github/workflows/codecov.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Go Test and Coverage

on: [push, pull_request]

jobs:
build:

runs-on: ubuntu-latest

steps:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.21

- name: Check out code into the Go module directory
uses: actions/checkout@v2

- name: Get dependencies
run: go get -v -t -d ./...

- name: Build
run: go build -v ./...

- name: Test and get coverage
run: go test -v ./... -coverprofile=coverage.txt

- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}


7 changes: 4 additions & 3 deletions integration/kvrocks_transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"
"time"

"github.com/kkkzoz/oreo/internal/mock"
"github.com/kkkzoz/oreo/internal/testutil"
"github.com/kkkzoz/oreo/internal/util"
"github.com/kkkzoz/oreo/pkg/config"
Expand Down Expand Up @@ -632,7 +633,7 @@ func TestKvrocks_SlowTransactionRecordExpiredWhenPrepare_Conflict(t *testing.T)

go func() {
slowTxn := txn.NewTransaction()
conn := redis.NewMockRedisConnection("localhost", 6666, 2, false,
conn := mock.NewMockRedisConnection("localhost", 6666, 2, false,
func() error { time.Sleep(2 * time.Second); return nil })
rds := redis.NewRedisDatastore(KVROCKS, conn)
slowTxn.AddDatastore(rds)
Expand Down Expand Up @@ -741,7 +742,7 @@ func TestKvrocks_SlowTransactionRecordExpiredWhenPrepare_NoConflict(t *testing.T

go func() {
slowTxn := txn.NewTransaction()
conn := redis.NewMockRedisConnection("localhost", 6666, 4, false,
conn := mock.NewMockRedisConnection("localhost", 6666, 4, false,
func() error { time.Sleep(2 * time.Second); return nil })
rds := redis.NewRedisDatastore(KVROCKS, conn)
slowTxn.AddDatastore(rds)
Expand Down Expand Up @@ -860,7 +861,7 @@ func TestKvrocks_TransactionAbortWhenWritingTSR(t *testing.T) {
}

txn := txn.NewTransaction()
conn := redis.NewMockRedisConnection("localhost", 6666, 5, true,
conn := mock.NewMockRedisConnection("localhost", 6666, 5, true,
func() error { time.Sleep(3 * time.Second); return errors.New("fail to write TSR") })
mds := redis.NewRedisDatastore(KVROCKS, conn)
txn.AddDatastore(mds)
Expand Down
7 changes: 4 additions & 3 deletions integration/mongo_transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"
"time"

"github.com/kkkzoz/oreo/internal/mock"
"github.com/kkkzoz/oreo/internal/testutil"
"github.com/kkkzoz/oreo/internal/util"
"github.com/kkkzoz/oreo/pkg/config"
Expand Down Expand Up @@ -629,7 +630,7 @@ func TestMongo_SlowTransactionRecordExpiredWhenPrepare_Conflict(t *testing.T) {

go func() {
slowTxn := txn.NewTransaction()
conn := mongo.NewMockMongoConnection("localhost", 27017, 2, false,
conn := mock.NewMockMongoConnection("localhost", 27017, 2, false,
func() error { time.Sleep(2 * time.Second); return nil })
mds := mongo.NewMongoDatastore(MONGO, conn)
slowTxn.AddDatastore(mds)
Expand Down Expand Up @@ -740,7 +741,7 @@ func TestMongo_SlowTransactionRecordExpiredWhenPrepare_NoConflict(t *testing.T)

go func() {
slowTxn := txn.NewTransaction()
conn := mongo.NewMockMongoConnection("localhost", 27017, 4, false,
conn := mock.NewMockMongoConnection("localhost", 27017, 4, false,
func() error { time.Sleep(2 * time.Second); return nil })
mds := mongo.NewMongoDatastore(MONGO, conn)
slowTxn.AddDatastore(mds)
Expand Down Expand Up @@ -861,7 +862,7 @@ func TestMongo_TransactionAbortWhenWritingTSR(t *testing.T) {
}

txn := txn.NewTransaction()
conn := mongo.NewMockMongoConnection("localhost", 27017, 5, true,
conn := mock.NewMockMongoConnection("localhost", 27017, 5, true,
func() error { time.Sleep(3 * time.Second); return errors.New("fail to write TSR") })
mds := mongo.NewMongoDatastore(MONGO, conn)
txn.AddDatastore(mds)
Expand Down
11 changes: 6 additions & 5 deletions integration/redis_transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"
"time"

"github.com/kkkzoz/oreo/internal/mock"
"github.com/kkkzoz/oreo/internal/testutil"
"github.com/kkkzoz/oreo/internal/util"
"github.com/kkkzoz/oreo/pkg/config"
Expand Down Expand Up @@ -645,7 +646,7 @@ func TestRedisSlowTransactionRecordExpiredWhenPrepare_Conflict(t *testing.T) {

go func() {
slowTxn := txn.NewTransaction()
conn := redis.NewMockRedisConnection("localhost", 6379, 2, false,
conn := mock.NewMockRedisConnection("localhost", 6379, 2, false,
func() error { time.Sleep(2 * time.Second); return nil })
rds := redis.NewRedisDatastore("redis", conn)
slowTxn.AddDatastore(rds)
Expand Down Expand Up @@ -754,7 +755,7 @@ func TestRedisSlowTransactionRecordExpiredWhenPrepare_NoConflict(t *testing.T) {

go func() {
slowTxn := txn.NewTransaction()
conn := redis.NewMockRedisConnection("localhost", 6379, 4, false,
conn := mock.NewMockRedisConnection("localhost", 6379, 4, false,
func() error { time.Sleep(2 * time.Second); return nil })
rds := redis.NewRedisDatastore("redis", conn)
slowTxn.AddDatastore(rds)
Expand Down Expand Up @@ -873,7 +874,7 @@ func TestRedisTransactionAbortWhenWritingTSR(t *testing.T) {
}

txn := txn.NewTransaction()
conn := redis.NewMockRedisConnection("localhost", 6379, 5, true,
conn := mock.NewMockRedisConnection("localhost", 6379, 5, true,
func() error { time.Sleep(3 * time.Second); return errors.New("fail to write TSR") })
mds := redis.NewRedisDatastore("redis", conn)
txn.AddDatastore(mds)
Expand Down Expand Up @@ -1047,7 +1048,7 @@ func TestRedisRollbackConflict(t *testing.T) {
go func() {
time.Sleep(100 * time.Millisecond)
txnA := txn.NewTransaction()
mockConn := redis.NewMockRedisConnection("localhost", 6379, 0, false,
mockConn := mock.NewMockRedisConnection("localhost", 6379, 0, false,
func() error { time.Sleep(2 * time.Second); return nil })
rds := redis.NewRedisDatastore("redis", mockConn)
txnA.AddDatastore(rds)
Expand Down Expand Up @@ -1115,7 +1116,7 @@ func TestRedisRollForwardConflict(t *testing.T) {
go func() {
time.Sleep(100 * time.Millisecond)
txnA := txn.NewTransaction()
mockConn := redis.NewMockRedisConnection("localhost", 6379, 0, false,
mockConn := mock.NewMockRedisConnection("localhost", 6379, 0, false,
func() error { time.Sleep(2 * time.Second); return nil })
rds := redis.NewRedisDatastore("redis", mockConn)
txnA.AddDatastore(rds)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package mongo
package mock

import (
"fmt"

"github.com/kkkzoz/oreo/pkg/datastore/mongo"
)

// MockMongoConnection is a mock of MongoConnection
// When Put is called, it will return error when debugCounter is 0
// Semantically, it means `Put()` call will succeed X times
type MockMongoConnection struct {
*MongoConnection
*mongo.MongoConnection
debugCounter int
debugFunc func() error
isReturned bool
Expand All @@ -17,7 +19,7 @@ type MockMongoConnection struct {

func NewMockMongoConnection(address string, port int, limit int,
isReturned bool, debugFunc func() error) *MockMongoConnection {
conn := NewMongoConnection(&ConnectionOptions{
conn := mongo.NewMongoConnection(&mongo.ConnectionOptions{
Address: fmt.Sprintf("mongodb://%s:%d", address, port),
DBName: "oreo",
CollectionName: "records",
Expand All @@ -31,7 +33,7 @@ func NewMockMongoConnection(address string, port int, limit int,
}
}

func (m *MockMongoConnection) ConditionalUpdate(key string, value MongoItem) error {
func (m *MockMongoConnection) ConditionalUpdate(key string, value mongo.MongoItem) error {
defer func() { m.debugCounter--; m.callTimes++ }()
if m.debugCounter == 0 {
if m.isReturned {
Expand All @@ -43,7 +45,7 @@ func (m *MockMongoConnection) ConditionalUpdate(key string, value MongoItem) err
return m.MongoConnection.ConditionalUpdate(key, value)
}

func (m *MockMongoConnection) PutItem(key string, value MongoItem) error {
func (m *MockMongoConnection) PutItem(key string, value mongo.MongoItem) error {
defer func() { m.debugCounter--; m.callTimes++ }()
if m.debugCounter == 0 {
if m.isReturned {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mongo
package mock

import (
"errors"
Expand All @@ -7,7 +7,7 @@ import (
"github.com/stretchr/testify/assert"
)

func TestDebugCounter(t *testing.T) {
func TestMongo_DebugCounter(t *testing.T) {
t.Run("test less than limit", func(t *testing.T) {

conn := NewMockMongoConnection("localhost", 27017, 10, true,
Expand Down Expand Up @@ -47,7 +47,7 @@ func TestDebugCounter(t *testing.T) {

}

func TestDebugFunc(t *testing.T) {
func TestMongo_DebugFunc(t *testing.T) {

t.Run("trigger debugFunc", func(t *testing.T) {
conn := NewMockMongoConnection("localhost", 27017, 3, true,
Expand Down Expand Up @@ -92,7 +92,7 @@ func TestDebugFunc(t *testing.T) {
})
}

func TestCallTimes(t *testing.T) {
func TestMongo_CallTimes(t *testing.T) {
testCases := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
for _, testCase := range testCases {
conn := NewMockMongoConnection("localhost", 27017, 20, true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package redis
package mock

import (
"fmt"

"github.com/kkkzoz/oreo/pkg/datastore/redis"
)

// MockRedisConnection is a mock of RedisConnection
// When Put is called, it will return error when debugCounter is 0
// Semantically, it means `Put()` call will succeed X times
type MockRedisConnection struct {
*RedisConnection
*redis.RedisConnection
debugCounter int
debugFunc func() error
isReturned bool
Expand All @@ -17,7 +19,7 @@ type MockRedisConnection struct {

func NewMockRedisConnection(address string, port int, limit int,
isReturned bool, debugFunc func() error) *MockRedisConnection {
conn := NewRedisConnection(&ConnectionOptions{
conn := redis.NewRedisConnection(&redis.ConnectionOptions{
Address: fmt.Sprintf("%s:%d", address, port),
})
return &MockRedisConnection{
Expand All @@ -29,7 +31,7 @@ func NewMockRedisConnection(address string, port int, limit int,
}
}

func (m *MockRedisConnection) ConditionalUpdate(key string, value RedisItem, doCreate bool) error {
func (m *MockRedisConnection) ConditionalUpdate(key string, value redis.RedisItem, doCreate bool) error {
defer func() { m.debugCounter--; m.callTimes++ }()
if m.debugCounter == 0 {
if m.isReturned {
Expand All @@ -41,7 +43,7 @@ func (m *MockRedisConnection) ConditionalUpdate(key string, value RedisItem, doC
return m.RedisConnection.ConditionalUpdate(key, value, doCreate)
}

func (m *MockRedisConnection) PutItem(key string, value RedisItem) error {
func (m *MockRedisConnection) PutItem(key string, value redis.RedisItem) error {
defer func() { m.debugCounter--; m.callTimes++ }()
if m.debugCounter == 0 {
if m.isReturned {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package redis
package mock

import (
"errors"
Expand All @@ -7,7 +7,7 @@ import (
"github.com/stretchr/testify/assert"
)

func TestDebugCounter(t *testing.T) {
func TestRedis_DebugCounter(t *testing.T) {
t.Run("test less than limit", func(t *testing.T) {

conn := NewMockRedisConnection("localhost", 6379, 10, true,
Expand Down Expand Up @@ -45,7 +45,7 @@ func TestDebugCounter(t *testing.T) {

}

func TestDebugFunc(t *testing.T) {
func TestRedis_DebugFunc(t *testing.T) {

t.Run("trigger debugFunc", func(t *testing.T) {
conn := NewMockRedisConnection("localhost", 6379, 3, true,
Expand Down Expand Up @@ -88,7 +88,7 @@ func TestDebugFunc(t *testing.T) {
})
}

func TestCallTimes(t *testing.T) {
func TestRedis_CallTimes(t *testing.T) {
testCases := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
for _, testCase := range testCases {
conn := NewMockRedisConnection("localhost", 6379, 20, true,
Expand Down
18 changes: 17 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ It provides:

+ An easy-to-use interface for distributed transactions.
+ Support heterogeneous key-value store.
+ Clear architecture.
+ Clean architecture.



Expand All @@ -43,6 +43,22 @@ It provides:

## Getting Started

### Installation

Oreo supports 2 last Go versions and requires a Go version with
[modules](https://github.com/golang/go/wiki/Modules) support. So make sure to initialize a Go module:

```shell
go mod init github.com/my/repo
```

Then install oreo:

```shell
go get github.com/kkkzoz/oreo
```


> Full example is in `examples/oreo_basic_with_memory_datastore`
```go
Expand Down

0 comments on commit 7e283a0

Please sign in to comment.