Compare commits

...

3 Commits

Author SHA1 Message Date
bandl 8ee8b8ed97 tawd 2021-08-24 23:02:56 +08:00
bandl 77cde0b2ef test(test-test): add test 2021-08-21 21:38:06 +08:00
bandl 3709b6f8e5 feat(test-test): add test func 2021-08-21 21:20:13 +08:00
4 changed files with 170 additions and 0 deletions

16
conf/middle.json Normal file
View File

@ -0,0 +1,16 @@
{
"middle": [
{
"name": "M1",
"IsOut": false,
"Weight": 1
},
{
"name": "M3",
"IsOut": false,
"Weight": 3,
"IP": "2e2qe",
"Port": 222
}
]
}

17
pkg/lru/lru/lru.go Normal file
View File

@ -0,0 +1,17 @@
package lru
import (
"strconv"
"time"
)
// 保证所有函数和变量最小作用域
func Test(a int) (string, error) {
// awd
return strconv.Itoa(a), nil
}
func Nows() string {
return time.Now().Format("2006-01-02")
}

18
pkg/lru/lru/lru_test.go Normal file
View File

@ -0,0 +1,18 @@
package lru
import (
"github.com/stretchr/testify/require"
"testing"
)
func TestTest(t *testing.T) {
ts , err := Test(2)
require.NoError(t, err)
require.Regexp(t, `\d`, ts)
ts, err = Test(3)
require.NoError(t, err)
require.Equal(t, "3", ts)
require.Regexp(t, `\d{4}-\d{2}-\d{2}`, Nows())
}

119
pkg/middle/interface.go Normal file
View File

@ -0,0 +1,119 @@
package middle
import "time"
type Value interface {
Get(key string) interface{}
Put(key string, val interface{}) error
}
type Data struct {
m map[string]interface{}
}
func (d *Data) Get(key string) interface{} {
return d.m[key]
}
func (d *Data) Put(key string, val interface{}) error {
d.m[key] = val
return nil
}
func NewData() *Data {
return &Data{
m: make(map[string]interface{}),
}
}
type MiddlewareConf struct {
Weight int
Middle Middleware
IsOut bool
}
type Driver struct {
conf []*MiddlewareConf
}
func (d *Driver) Stat(value Value) error {
for _, m := range d.conf {
m.Middle.Put(value) // 协程
value = m.Middle.Out()
}
return nil
}
func NewDriver() *Driver {
return new(Driver)
}
func main() {
conf := []*MiddlewareConf{
{
Weight: 1,
Middle: NewM1(),
IsOut: false,
},
{
Weight: 2,
Middle: NewM2(),
IsOut: true,
},
}
driver := NewDriver()
driver.conf = conf
// 调用中间件驱动
val := NewData()
val.Put("2", 2)
driver.Stat(val)
}
type Middleware interface {
Put(val Value)
Out() Value
}
type M1 struct {
ch chan *Data
}
func (m *M1) Put(val Value) {
time.Sleep(200 * time.Second)
da := NewData()
da.Put("1", val)
m.ch <- da
}
func (m *M1) Out() Value {
return <-m.ch
}
func NewM1() *M1 {
return &M1{
make(chan *Data),
}
}
type M2 struct {
da Value
}
func (m *M2) Put(val Value) {
da := NewData()
m.da = da
}
func (m *M2) Out() Value {
m.da.Put("2", "plp")
return m.da
}
func NewM2() *M2 {
return &M2{}
}