Skip to content

Commit

Permalink
move driver/integrationtest to inner mod (#103)
Browse files Browse the repository at this point in the history
* update common package

* update

* update

* set go as 1.19

* add integration test

* let integrationtest cover dcron

* update test script

* update

* update fix

* update gocovmerge to v1.0.4

* update gomod
  • Loading branch information
dxyinme authored Aug 26, 2024
1 parent 5558701 commit e54b943
Show file tree
Hide file tree
Showing 45 changed files with 1,596 additions and 680 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ on:
branches: [ "master", "develop" ]
pull_request:
branches: [ "master", "develop" ]
# manually start testing.
workflow_dispatch:

jobs:

Expand All @@ -28,7 +30,7 @@ jobs:
go-version: ${{ matrix.go_version }}

- name: Test
run: go test -v -timeout 30m -coverprofile=coverage.txt -covermode=atomic ./...
run: bash scripts/run_all_tests.sh

- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v4
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
.idea/*
bin/
coverage.txt
coverage.html
coverage.html
cov/
3 changes: 3 additions & 0 deletions commons/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# COMMONS

This is a mod for everyone who want to customize theirself dcron drivers.
File renamed without changes.
21 changes: 2 additions & 19 deletions driver/driver.go → commons/driver.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
package driver
package commons

import (
"context"

redis "github.com/redis/go-redis/v9"
clientv3 "go.etcd.io/etcd/client/v3"
)
import "context"

// There is only one driver for one dcron.
// Tips for write a user-defined Driver by yourself.
Expand All @@ -29,15 +24,3 @@ type DriverV2 interface {

WithOption(opt Option) (err error)
}

func NewRedisDriver(redisClient redis.UniversalClient) DriverV2 {
return newRedisDriver(redisClient)
}

func NewEtcdDriver(etcdCli *clientv3.Client) DriverV2 {
return newEtcdDriver(etcdCli)
}

func NewRedisZSetDriver(redisClient redis.UniversalClient) DriverV2 {
return newRedisZSetDriver(redisClient)
}
5 changes: 5 additions & 0 deletions commons/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/libi/dcron/commons

go 1.19

require github.com/google/uuid v1.5.0
2 changes: 2 additions & 0 deletions commons/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
12 changes: 6 additions & 6 deletions driver/option.go → commons/option.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package driver
package commons

import (
"time"

"github.com/libi/dcron/dlog"
"github.com/libi/dcron/commons/dlog"
)

const (
Expand All @@ -15,12 +15,12 @@ type Option interface {
Type() int
}

type TimeoutOption struct{ timeout time.Duration }
type TimeoutOption struct{ Timeout time.Duration }

func (to TimeoutOption) Type() int { return OptionTypeTimeout }
func NewTimeoutOption(timeout time.Duration) TimeoutOption { return TimeoutOption{timeout: timeout} }
func NewTimeoutOption(timeout time.Duration) TimeoutOption { return TimeoutOption{Timeout: timeout} }

type LoggerOption struct{ logger dlog.Logger }
type LoggerOption struct{ Logger dlog.Logger }

func (to LoggerOption) Type() int { return OptionTypeLogger }
func NewLoggerOption(logger dlog.Logger) LoggerOption { return LoggerOption{logger: logger} }
func NewLoggerOption(logger dlog.Logger) LoggerOption { return LoggerOption{Logger: logger} }
2 changes: 1 addition & 1 deletion driver/util.go → commons/util.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package driver
package commons

import (
"time"
Expand Down
2 changes: 1 addition & 1 deletion cron/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"sync"
"time"

"github.com/libi/dcron/dlog"
"github.com/libi/dcron/commons/dlog"
)

// JobWrapper decorates the given Job with some behavior.
Expand Down
2 changes: 1 addition & 1 deletion cron/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"testing"
"time"

"github.com/libi/dcron/dlog"
"github.com/libi/dcron/commons/dlog"
)

func appendingJob(slice *[]int, value int) Job {
Expand Down
2 changes: 1 addition & 1 deletion cron/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"sync"
"time"

"github.com/libi/dcron/dlog"
"github.com/libi/dcron/commons/dlog"
)

// Cron keeps track of any number of entries, invoking the associated func as
Expand Down
2 changes: 1 addition & 1 deletion cron/cron_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"testing"
"time"

"github.com/libi/dcron/dlog"
"github.com/libi/dcron/commons/dlog"
)

// Many tests schedule a job for every second, and then wait at most a second
Expand Down
2 changes: 1 addition & 1 deletion cron/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"log"
"os"

"github.com/libi/dcron/dlog"
"github.com/libi/dcron/commons/dlog"
)

// DefaultLogger is used by Cron if none is specified.
Expand Down
2 changes: 1 addition & 1 deletion cron/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cron
import (
"time"

"github.com/libi/dcron/dlog"
"github.com/libi/dcron/commons/dlog"
)

// Option represents a modification to the default behavior of a Cron.
Expand Down
8 changes: 4 additions & 4 deletions dcron.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
"sync/atomic"
"time"

"github.com/libi/dcron/commons"
"github.com/libi/dcron/commons/dlog"
"github.com/libi/dcron/cron"
"github.com/libi/dcron/dlog"
"github.com/libi/dcron/driver"
)

const (
Expand Down Expand Up @@ -59,7 +59,7 @@ type Dcron struct {
}

// NewDcron create a Dcron
func NewDcron(serverName string, driver driver.DriverV2, cronOpts ...cron.Option) *Dcron {
func NewDcron(serverName string, driver commons.DriverV2, cronOpts ...cron.Option) *Dcron {
dcron := newDcron(serverName)
dcron.crOptions = cronOpts
dcron.cr = cron.New(cronOpts...)
Expand All @@ -69,7 +69,7 @@ func NewDcron(serverName string, driver driver.DriverV2, cronOpts ...cron.Option
}

// NewDcronWithOption create a Dcron with Dcron Option
func NewDcronWithOption(serverName string, driver driver.DriverV2, dcronOpts ...Option) *Dcron {
func NewDcronWithOption(serverName string, driver commons.DriverV2, dcronOpts ...Option) *Dcron {
dcron := newDcron(serverName)
for _, opt := range dcronOpts {
opt(dcron)
Expand Down
25 changes: 13 additions & 12 deletions driver/etcddriver.go → driver/etcddriver/etcddriver.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package driver
package etcddriver

import (
"context"
"log"
"sync"
"time"

"github.com/libi/dcron/dlog"
"github.com/libi/dcron/commons"
"github.com/libi/dcron/commons/dlog"
"go.etcd.io/etcd/api/v3/mvccpb"
clientv3 "go.etcd.io/etcd/client/v3"
)
Expand Down Expand Up @@ -34,7 +35,7 @@ type EtcdDriver struct {
}

// NewEtcdDriver
func newEtcdDriver(cli *clientv3.Client) *EtcdDriver {
func NewDriver(cli *clientv3.Client) *EtcdDriver {
ser := &EtcdDriver{
cli: cli,
nodes: &sync.Map{},
Expand Down Expand Up @@ -70,7 +71,7 @@ func (e *EtcdDriver) putKeyWithLease(ctx context.Context, key, val string) (clie

// WatchService 初始化服务列表和监视
func (e *EtcdDriver) watchService(ctx context.Context, serviceName string) error {
prefix := GetKeyPre(serviceName)
prefix := commons.GetKeyPre(serviceName)
// 根据前缀获取现有的key
resp, err := e.cli.Get(ctx, prefix, clientv3.WithPrefix())
if err != nil {
Expand All @@ -88,7 +89,7 @@ func (e *EtcdDriver) watchService(ctx context.Context, serviceName string) error

// watcher 监听前缀
func (e *EtcdDriver) watcher(serviceName string) {
prefix := GetKeyPre(serviceName)
prefix := commons.GetKeyPre(serviceName)
rch := e.cli.Watch(context.Background(), prefix, clientv3.WithPrefix())
for wresp := range rch {
for _, ev := range wresp.Events {
Expand Down Expand Up @@ -170,9 +171,9 @@ func (e *EtcdDriver) keepHeartBeat() {
}
}

func (e *EtcdDriver) Init(serverName string, opts ...Option) {
func (e *EtcdDriver) Init(serverName string, opts ...commons.Option) {
e.serviceName = serverName
e.nodeID = GetNodeId(serverName)
e.nodeID = commons.GetNodeId(serverName)
for _, opt := range opts {
e.WithOption(opt)
}
Expand Down Expand Up @@ -204,15 +205,15 @@ func (e *EtcdDriver) Stop(ctx context.Context) (err error) {
return
}

func (e *EtcdDriver) WithOption(opt Option) (err error) {
func (e *EtcdDriver) WithOption(opt commons.Option) (err error) {
switch opt.Type() {
case OptionTypeTimeout:
case commons.OptionTypeTimeout:
{
e.lease = int64(opt.(TimeoutOption).timeout.Seconds())
e.lease = int64(opt.(commons.TimeoutOption).Timeout.Seconds())
}
case OptionTypeLogger:
case commons.OptionTypeLogger:
{
e.logger = opt.(LoggerOption).logger
e.logger = opt.(commons.LoggerOption).Logger
}
}
return
Expand Down
19 changes: 10 additions & 9 deletions driver/etcddriver_test.go → driver/etcddriver/etcddriver_test.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,37 @@
package driver_test
package etcddriver_test

import (
"context"
"testing"
"time"

"github.com/libi/dcron/dlog"
"github.com/libi/dcron/driver"
"github.com/libi/dcron/commons"
"github.com/libi/dcron/commons/dlog"
"github.com/libi/dcron/driver/etcddriver"
"github.com/stretchr/testify/require"
clientv3 "go.etcd.io/etcd/client/v3"
"go.etcd.io/etcd/tests/v3/integration"
)

func testFuncNewEtcdDriver(cfg clientv3.Config) driver.DriverV2 {
func testFuncNewEtcdDriver(cfg clientv3.Config) *etcddriver.EtcdDriver {
cli, err := clientv3.New(cfg)
if err != nil {
panic(err)
}
return driver.NewEtcdDriver(cli)
return etcddriver.NewDriver(cli)
}

func TestEtcdDriver_GetNodes(t *testing.T) {
etcdsvr := integration.NewLazyCluster()
defer etcdsvr.Terminate()
N := 10
drvs := make([]driver.DriverV2, 0)
drvs := make([]*etcddriver.EtcdDriver, 0)
for i := 0; i < N; i++ {
drv := testFuncNewEtcdDriver(clientv3.Config{
Endpoints: etcdsvr.EndpointsV3(),
DialTimeout: 3 * time.Second,
})
drv.Init(t.Name(), driver.NewTimeoutOption(5*time.Second), driver.NewLoggerOption(dlog.NewLoggerForTest(t)))
drv.Init(t.Name(), commons.NewTimeoutOption(5*time.Second), commons.NewLoggerOption(dlog.NewLoggerForTest(t)))
err := drv.Start(context.Background())
require.Nil(t, err)
drvs = append(drvs, drv)
Expand All @@ -57,13 +58,13 @@ func TestEtcdDriver_Stop(t *testing.T) {
Endpoints: etcdsvr.EndpointsV3(),
DialTimeout: 3 * time.Second,
})
drv1.Init(t.Name(), driver.NewTimeoutOption(5*time.Second), driver.NewLoggerOption(dlog.NewLoggerForTest(t)))
drv1.Init(t.Name(), commons.NewTimeoutOption(5*time.Second), commons.NewLoggerOption(dlog.NewLoggerForTest(t)))

drv2 := testFuncNewEtcdDriver(clientv3.Config{
Endpoints: etcdsvr.EndpointsV3(),
DialTimeout: 3 * time.Second,
})
drv2.Init(t.Name(), driver.NewTimeoutOption(5*time.Second), driver.NewLoggerOption(dlog.NewLoggerForTest(t)))
drv2.Init(t.Name(), commons.NewTimeoutOption(5*time.Second), commons.NewLoggerOption(dlog.NewLoggerForTest(t)))
require.Nil(t, drv2.Start(context.Background()))
require.Nil(t, drv1.Start(context.Background()))

Expand Down
Loading

0 comments on commit e54b943

Please sign in to comment.