forked from p93542168/wheat-cache
test(middleware_driver): add middleware driver test
This commit is contained in:
parent
7c6c26c5fa
commit
ec78e6bfd5
|
@ -1,7 +1,16 @@
|
|||
package middle
|
||||
|
||||
import getMiddlewareMap "gitee.com/timedb/wheatCache/plugins/config"
|
||||
import (
|
||||
"sync"
|
||||
|
||||
getMiddlewareMap "gitee.com/timedb/wheatCache/plugins/config"
|
||||
)
|
||||
|
||||
func Init() {
|
||||
getMiddlewareMap.GetMiddlewareMap()
|
||||
}
|
||||
|
||||
var (
|
||||
oneMiddle sync.Once
|
||||
MiddleWareDriver *MiddleWare
|
||||
)
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
package middle
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gitee.com/timedb/wheatCache/pkg/event"
|
||||
)
|
||||
|
||||
type MiddleWare struct {
|
||||
eventDriver event.DriverInterface
|
||||
eventConsumer event.ConsumerInterface
|
||||
}
|
||||
|
||||
func NewMiddleWare() *MiddleWare {
|
||||
oneMiddle.Do(func() {
|
||||
driver := event.NewDriver(1000)
|
||||
MiddleWareDriver = &MiddleWare{
|
||||
eventDriver: driver,
|
||||
eventConsumer: event.NewConsumer(driver),
|
||||
}
|
||||
})
|
||||
return MiddleWareDriver
|
||||
}
|
||||
|
||||
func (m *MiddleWare) GetEventDriver() event.DriverInterface {
|
||||
return m.eventDriver
|
||||
}
|
||||
|
||||
func (m *MiddleWare) work(ctx context.Context) {
|
||||
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
package middle
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"gitee.com/timedb/wheatCache/pkg/event"
|
||||
)
|
||||
|
||||
func Test_middleware_driver(t *testing.T) {
|
||||
|
||||
ctx := context.Background()
|
||||
type Library struct {
|
||||
driver MiddleWare
|
||||
}
|
||||
|
||||
library := &Library{
|
||||
driver: *NewMiddleWare(),
|
||||
}
|
||||
|
||||
|
||||
type A struct {
|
||||
produce event.ProduceInterface
|
||||
}
|
||||
|
||||
a := &A{
|
||||
produce: event.NewProduce(library.driver.eventDriver),
|
||||
}
|
||||
|
||||
type B struct {
|
||||
cumsumer event.ConsumerInterface
|
||||
}
|
||||
|
||||
b := &B{
|
||||
cumsumer: event.NewConsumer(library.driver.eventDriver),
|
||||
}
|
||||
|
||||
book := event.NewEvent("book")
|
||||
book.SetMsg("title", "goland")
|
||||
book.SetCtxValue("page", 100)
|
||||
|
||||
go func() {
|
||||
a.produce.Call(ctx, book)
|
||||
}()
|
||||
|
||||
b_book := b.cumsumer.Receive(ctx)
|
||||
fmt.Println(b_book.GetMsg("title"))
|
||||
}
|
Loading…
Reference in New Issue