Skip to content

Commit

Permalink
完善测试
Browse files Browse the repository at this point in the history
  • Loading branch information
yumaojun03 committed Dec 24, 2023
1 parent f706c63 commit 05c26e8
Show file tree
Hide file tree
Showing 12 changed files with 140 additions and 0 deletions.
33 changes: 33 additions & 0 deletions examples/cache/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main

import (
"context"

"github.com/infraboard/mcube/v2/ioc/config/cache"
)

type TestStruct struct {
FiledA string
}

func main() {
ctx := context.Background()

c := cache.C()

key := "test"
obj := &TestStruct{FiledA: "test"}

// 设置缓存
err := c.Set(ctx, key, obj, cache.WithExpiration(300))
if err != nil {
panic(err)
}

// 后期缓存
var v *TestStruct
err = c.Get(ctx, key, v)
if err != nil {
panic(err)
}
}
17 changes: 17 additions & 0 deletions examples/kafka/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

import (
"fmt"

"github.com/infraboard/mcube/v2/ioc/config/kafka"
)

func main() {
// 消息生产者
producer := kafka.Producer("test")
fmt.Println(producer)

// 消息消费者
consumer := kafka.ConsumerGroup("group id", []string{"topic name"})
fmt.Println(consumer)
}
23 changes: 23 additions & 0 deletions examples/lock/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"context"
"time"

"github.com/infraboard/mcube/v2/ioc/config/lock"
)

func main() {
// 创建一个key为test, 超时时间为10秒的锁
locker := lock.L().New("test", 10*time.Second)

ctx := context.Background()

// 获取锁
if err := locker.Lock(ctx); err != nil {
panic(err)
}

// 释放锁
defer locker.UnLock(ctx)
}
12 changes: 12 additions & 0 deletions examples/redis/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

import (
"fmt"

"github.com/infraboard/mcube/v2/ioc/config/redis"
)

func main() {
client := redis.Client()
fmt.Println(client)
}
9 changes: 9 additions & 0 deletions ioc/config/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/infraboard/mcube/v2/ioc"
"github.com/infraboard/mcube/v2/ioc/config/cache"
"github.com/infraboard/mcube/v2/tools/file"
)

var (
Expand All @@ -28,6 +29,14 @@ func TestGetClientGetter(t *testing.T) {
t.Log(b)
}

func TestDefaultConfig(t *testing.T) {
file.MustToToml(
cache.AppName,
ioc.Config().Get(cache.AppName),
"test/default.toml",
)
}

func init() {
err := ioc.ConfigIocObject(ioc.NewLoadConfigRequest())
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions ioc/config/cache/test/default.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[cache]
provider = "go_cache"
ttl = 300
9 changes: 9 additions & 0 deletions ioc/config/kafka/kafka_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/infraboard/mcube/v2/ioc"
"github.com/infraboard/mcube/v2/ioc/config/kafka"
"github.com/infraboard/mcube/v2/tools/file"
)

func TestKafkaProducer(t *testing.T) {
Expand All @@ -17,6 +18,14 @@ func TestKafkaConsumerGroup(t *testing.T) {
t.Log(m)
}

func TestDefaultConfig(t *testing.T) {
file.MustToToml(
kafka.AppName,
ioc.Config().Get(kafka.AppName),
"test/default.toml",
)
}

func init() {
err := ioc.ConfigIocObject(ioc.NewLoadConfigRequest())
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions ioc/config/kafka/test/default.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[kafka]
brokers = ["127.0.0.1:9092"]
scram_algorithm = "SHA512"
username = ""
password = ""
debug = false
10 changes: 10 additions & 0 deletions ioc/config/lock/lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"testing"
"time"

"github.com/infraboard/mcube/v2/ioc"
"github.com/infraboard/mcube/v2/ioc/config/lock"
"github.com/infraboard/mcube/v2/tools/file"
)

var (
Expand All @@ -20,3 +22,11 @@ func TestRedisLock(t *testing.T) {
t.Fatal(err)
}
}

func TestDefaultConfig(t *testing.T) {
file.MustToToml(
lock.AppName,
ioc.Config().Get(lock.AppName),
"test/default.toml",
)
}
2 changes: 2 additions & 0 deletions ioc/config/lock/test/default.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[lock]
provider = "redis"
9 changes: 9 additions & 0 deletions ioc/config/redis/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,22 @@ import (

"github.com/infraboard/mcube/v2/ioc"
"github.com/infraboard/mcube/v2/ioc/config/redis"
"github.com/infraboard/mcube/v2/tools/file"
)

func TestRedisClient(t *testing.T) {
m := redis.Client()
t.Log(m)
}

func TestDefaultConfig(t *testing.T) {
file.MustToToml(
redis.AppName,
ioc.Config().Get(redis.AppName),
"test/default.toml",
)
}

func init() {

err := ioc.ConfigIocObject(ioc.NewLoadConfigRequest())
Expand Down
7 changes: 7 additions & 0 deletions ioc/config/redis/test/default.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[redis]
endpoints = ["127.0.0.1:6379"]
database = 0
username = ""
password = ""
enable_trace = true
enable_metrics = false

0 comments on commit 05c26e8

Please sign in to comment.