Skip to content

Commit

Permalink
Merge pull request #173
Browse files Browse the repository at this point in the history
* fix: clarify env usage

* fix: setup test correctly
  • Loading branch information
Reasno authored Aug 4, 2021
1 parent 5f58dad commit d2abd07
Show file tree
Hide file tree
Showing 45 changed files with 320 additions and 271 deletions.
26 changes: 13 additions & 13 deletions config/remote/remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,23 @@ import (
"context"
"fmt"
"os"
"strings"
"sync"
"testing"
"time"

"github.com/DoNewsCode/core/internal"
"github.com/stretchr/testify/assert"
"go.etcd.io/etcd/client/v3"
)

var envDefaultEtcdAddrs, envDefaultEtcdAddrsIsSet = internal.GetDefaultAddrsFromEnv("ETCD_ADDR", "127.0.0.1:2379")

func TestMain(m *testing.M) {
if !envDefaultEtcdAddrsIsSet {
fmt.Println("Set env ETCD_ADDR to run remote tests")
os.Exit(0)
}
os.Exit(m.Run())
}

func TestRemote(t *testing.T) {
if os.Getenv("ETCD_ADDR") == "" {
t.Skip("set ETCD_ADDR to run TestRemote")
return
}
addrs := strings.Split(os.Getenv("ETCD_ADDR"), ",")
cfg := &clientv3.Config{
Endpoints: envDefaultEtcdAddrs,
Endpoints: addrs,
DialTimeout: 2 * time.Second,
}

Expand Down Expand Up @@ -66,6 +61,11 @@ func TestRemote(t *testing.T) {
}

func TestError(t *testing.T) {
if os.Getenv("ETCD_ADDR") == "" {
t.Skip("set ETCD_ADDR to run TestError")
return
}
addrs := strings.Split(os.Getenv("ETCD_ADDR"), ",")
var (
r *Remote
err error
Expand All @@ -89,7 +89,7 @@ func TestError(t *testing.T) {
assert.Error(t, err)

cfg = &clientv3.Config{
Endpoints: envDefaultEtcdAddrs,
Endpoints: addrs,
DialTimeout: 2 * time.Second,
}
r = Provider("config-test1", cfg)
Expand Down
11 changes: 4 additions & 7 deletions dtx/sagas/mysqlstore/mysql_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/DoNewsCode/core"
"github.com/DoNewsCode/core/dtx"
"github.com/DoNewsCode/core/dtx/sagas"
"github.com/DoNewsCode/core/internal"
"github.com/DoNewsCode/core/otgorm"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
Expand All @@ -24,17 +23,15 @@ func (m module) ProvideMigration() []*otgorm.Migration {
return Migrations("default")
}

var envDefaultMysqlDsn, envDefaultMysqlDsnIsSet = internal.GetDefaultAddrFromEnv("MYSQL_DSN", "root@tcp(127.0.0.1:3306)/app?charset=utf8mb4&parseTime=True&loc=Local")

func TestMain(m *testing.M) {
if !envDefaultMysqlDsnIsSet {
if os.Getenv("MYSQL_DSN") == "" {
fmt.Println("Set env MYSQL_DSN to run mysqlstore tests")
os.Exit(0)
}
c := core.New(
core.WithInline("log.level", "error"),
core.WithInline("gorm.default.database", "mysql"),
core.WithInline("gorm.default.dsn", envDefaultMysqlDsn),
core.WithInline("gorm.default.dsn", os.Getenv("MYSQL_DSN")),
)
c.ProvideEssentials()
c.Provide(otgorm.Providers())
Expand Down Expand Up @@ -265,7 +262,7 @@ func TestMySQLStore(t *testing.T) {
c := core.New(
core.WithInline("log.level", "debug"),
core.WithInline("gorm.default.database", "mysql"),
core.WithInline("gorm.default.dsn", envDefaultMysqlDsn),
core.WithInline("gorm.default.dsn", os.Getenv("MYSQL_DSN")),
)
c.ProvideEssentials()
c.Provide(otgorm.Providers())
Expand All @@ -280,7 +277,7 @@ func TestStore_CleanUp(t *testing.T) {
c := core.New(
core.WithInline("log.level", "error"),
core.WithInline("gorm.default.database", "mysql"),
core.WithInline("gorm.default.dsn", envDefaultMysqlDsn),
core.WithInline("gorm.default.dsn", os.Getenv("MYSQL_DSN")),
)
c.ProvideEssentials()
c.Provide(otgorm.Providers())
Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ require (
github.com/go-gormigrate/gormigrate/v2 v2.0.0
github.com/go-kit/kit v0.11.0
github.com/go-redis/redis/v8 v8.6.0
github.com/gogo/protobuf v1.3.2
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/mock v1.5.0
github.com/golang/protobuf v1.5.2
github.com/golang/protobuf v1.5.2 // indirect
github.com/gorilla/handlers v1.5.1
github.com/gorilla/mux v1.8.0
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
Expand Down Expand Up @@ -50,8 +50,8 @@ require (
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
gorm.io/driver/clickhouse v0.1.0
gorm.io/driver/mysql v1.0.4
gorm.io/driver/postgres v1.1.0
gorm.io/driver/postgres v1.1.0 // indirect
gorm.io/driver/sqlite v1.1.4
gorm.io/driver/sqlserver v1.0.7
gorm.io/driver/sqlserver v1.0.7 // indirect
gorm.io/gorm v1.21.10
)
22 changes: 0 additions & 22 deletions internal/env.go

This file was deleted.

15 changes: 13 additions & 2 deletions leader/dependency_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package leader

import (
"os"
"strings"
"testing"

"github.com/DoNewsCode/core/config"
leaderetcd2 "github.com/DoNewsCode/core/leader/leaderetcd"
"github.com/DoNewsCode/core/key"
"github.com/DoNewsCode/core/leader/leaderetcd"
"github.com/stretchr/testify/assert"
"go.etcd.io/etcd/client/v3"
)
Expand All @@ -19,7 +22,15 @@ func (m *mockMaker) Make(name string) (*clientv3.Client, error) {
}

func TestDetermineDriver(t *testing.T) {
driver := &leaderetcd2.EtcdDriver{}
if os.Getenv("ETCD_ADDR") == "" {
t.Skip("set ETCD_ADDR to run TestDetermineDriver")
return
}
addrs := strings.Split(os.Getenv("ETCD_ADDR"), ",")
client, _ := clientv3.New(clientv3.Config{
Endpoints: addrs,
})
driver := leaderetcd.NewEtcdDriver(client, key.New())
p := in{}
p.Driver = driver
determineDriver(&p)
Expand Down
20 changes: 7 additions & 13 deletions leader/election_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,29 @@ package leader

import (
"context"
"fmt"
"os"
"strings"
"testing"
"time"

"github.com/DoNewsCode/core/events"
"github.com/DoNewsCode/core/internal"
"github.com/DoNewsCode/core/key"
leaderetcd2 "github.com/DoNewsCode/core/leader/leaderetcd"
"github.com/stretchr/testify/assert"
"go.etcd.io/etcd/client/v3"
"go.uber.org/atomic"
)

var envDefaultEtcdAddrs, envDefaultEtcdAddrsIsSet = internal.GetDefaultAddrsFromEnv("ETCD_ADDR", "127.0.0.1:2379")

func TestMain(m *testing.M) {
if !envDefaultEtcdAddrsIsSet {
fmt.Println("Set env ETCD_ADDR to run leader tests")
os.Exit(0)
}
os.Exit(m.Run())
}

func TestElection(t *testing.T) {
if os.Getenv("ETCD_ADDR") == "" {
t.Skip("set ETCD_ADDR to run TestElection")
return
}
addrs := strings.Split(os.Getenv("ETCD_ADDR"), ",")
var dispatcher = &events.SyncDispatcher{}
var e1, e2 Election

client, err := clientv3.New(clientv3.Config{Endpoints: envDefaultEtcdAddrs, DialTimeout: 2 * time.Second})
client, err := clientv3.New(clientv3.Config{Endpoints: addrs, DialTimeout: 2 * time.Second})
assert.NoError(t, err)
defer client.Close()

Expand Down
5 changes: 5 additions & 0 deletions leader/example_cronjob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package leader_test
import (
"context"
"fmt"
"os"

"github.com/DoNewsCode/core"
"github.com/DoNewsCode/core/di"
Expand All @@ -24,6 +25,10 @@ func (s CronModule) ProvideCron(crontab *cron.Cron) {
}

func Example_cronjob() {
if os.Getenv("ETCD_ADDR") == "" {
fmt.Println("set ETCD_ADDR to run this example")
return
}
c := core.Default(core.WithInline("log.level", "none"))
c.Provide(di.Deps{func() *cron.Cron {
return cron.New(cron.WithSeconds())
Expand Down
5 changes: 5 additions & 0 deletions leader/example_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net/http"
"os"
"time"

"github.com/DoNewsCode/core"
Expand All @@ -29,6 +30,10 @@ func (s ServerModule) ProvideHTTP(router *mux.Router) {
}

func Example_server() {
if os.Getenv("ETCD_ADDR") == "" {
fmt.Println("set ETCD_ADDR to run this example")
return
}
c := core.Default(core.WithInline("log.level", "none"))
c.Provide(otetcd.Providers())
c.Provide(leader.Providers())
Expand Down
20 changes: 7 additions & 13 deletions leader/leaderetcd/etcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,23 @@ package leaderetcd

import (
"context"
"fmt"
"os"
"strings"
"testing"
"time"

"github.com/DoNewsCode/core/internal"
"github.com/DoNewsCode/core/key"
"github.com/stretchr/testify/assert"
"go.etcd.io/etcd/client/v3"
)

var envDefaultEtcdAddrs, envDefaultEtcdAddrsIsSet = internal.GetDefaultAddrsFromEnv("ETCD_ADDR", "127.0.0.1:2379")

func TestMain(m *testing.M) {
if !envDefaultEtcdAddrsIsSet {
fmt.Println("Set env ETCD_ADDR to run leaderetcd tests")
os.Exit(0)
}
os.Exit(m.Run())
}

func TestNewEtcdDriver(t *testing.T) {
client, err := clientv3.New(clientv3.Config{Endpoints: envDefaultEtcdAddrs, DialTimeout: 2 * time.Second})
if os.Getenv("ETCD_ADDR") == "" {
t.Skip("set ETCD_ADDR to run TestNewEtcdDriver")
return
}
addrs := strings.Split(os.Getenv("ETCD_ADDR"), ",")
client, err := clientv3.New(clientv3.Config{Endpoints: addrs, DialTimeout: 2 * time.Second})
assert.NoError(t, err)
defer client.Close()

Expand Down
29 changes: 13 additions & 16 deletions leader/leaderredis/redis_test.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,26 @@
// +build integration

package leaderredis

import (
"context"
"fmt"
"os"
"strings"
"testing"
"time"

"github.com/DoNewsCode/core/events"
"github.com/DoNewsCode/core/internal"
"github.com/DoNewsCode/core/key"
"github.com/DoNewsCode/core/leader"
"github.com/go-redis/redis/v8"
"github.com/stretchr/testify/assert"
)

var envDefaultRedisAddrs, envDefaultRedisAddrsIsSet = internal.GetDefaultAddrsFromEnv("REDIS_ADDR", "127.0.0.1:6379")

func TestMain(m *testing.M) {
if !envDefaultRedisAddrsIsSet {
fmt.Println("Set env REDIS_ADDR to run leaderredis tests")
os.Exit(0)
}
os.Exit(m.Run())
}

func TestCampaign(t *testing.T) {
client := redis.NewUniversalClient(&redis.UniversalOptions{Addrs: envDefaultRedisAddrs})
if os.Getenv("REDIS_ADDR") == "" {
t.Skip("set REDIS_ADDR to run TestCampaign")
return
}
addrs := strings.Split(os.Getenv("REDIS_ADDR"), ",")
client := redis.NewUniversalClient(&redis.UniversalOptions{Addrs: addrs})
driver := RedisDriver{
client: client,
keyer: key.New(),
Expand All @@ -51,9 +43,14 @@ func TestNewRedisDriver(t *testing.T) {
}

func TestElection(t *testing.T) {
if os.Getenv("REDIS_ADDR") == "" {
t.Skip("set REDIS_ADDR to run TestCampaign")
return
}
addrs := strings.Split(os.Getenv("REDIS_ADDR"), ",")
var dispatcher = &events.SyncDispatcher{}
var e1, e2 *leader.Election
var driver = NewRedisDriver(redis.NewUniversalClient(&redis.UniversalOptions{}), key.New("testElection"), WithPollInterval(time.Millisecond), WithExpiration(time.Second))
var driver = NewRedisDriver(redis.NewUniversalClient(&redis.UniversalOptions{Addrs: addrs}), key.New("testElection"), WithPollInterval(time.Millisecond), WithExpiration(time.Second))

e1 = leader.NewElection(dispatcher, driver)
e2 = leader.NewElection(dispatcher, driver)
Expand Down
7 changes: 2 additions & 5 deletions otes/dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"github.com/DoNewsCode/core/config"
"github.com/DoNewsCode/core/contract"
"github.com/DoNewsCode/core/di"
"github.com/DoNewsCode/core/internal"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/olivere/elastic/v7"
Expand All @@ -14,8 +13,6 @@ import (
"net/http"
)

var envDefaultElasticsearchAddrs, envDefaultElasticsearchAddrsIsSet = internal.GetDefaultAddrsFromEnv("ELASTICSEARCH_ADDR", "http://127.0.0.1:9200")

/*
Providers returns a set of dependency providers. It includes the Maker, the
default *elastic.Client and exported configs.
Expand Down Expand Up @@ -70,7 +67,7 @@ func provideEsFactory(p factoryIn) (factoryOut, func()) {
if name != "default" {
return di.Pair{}, fmt.Errorf("elastic configuration %s not valid: %w", name, err)
}
conf.URL = envDefaultElasticsearchAddrs
conf.URL = []string{"http://127.0.0.1:9200"}
}
if p.Interceptor != nil {
p.Interceptor(name, &conf)
Expand Down Expand Up @@ -143,7 +140,7 @@ func provideConfig() configOut {
Data: map[string]interface{}{
"es": map[string]Config{
"default": {
URL: envDefaultElasticsearchAddrs,
URL: []string{"http://127.0.0.1:9200"},
Shards: 1,
},
},
Expand Down
Loading

0 comments on commit d2abd07

Please sign in to comment.