Skip to content

Commit

Permalink
新文件: routine/channel.go
Browse files Browse the repository at this point in the history
新文件:   routine/channel_test.go
  • Loading branch information
ssst0n3 committed Sep 14, 2020
1 parent 0b6165b commit 627c81c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
20 changes: 20 additions & 0 deletions routine/channel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package routine

type Result struct {
Key string
Value interface{}
}

func Channel2Map(c chan Result, m map[string]Result) {
for {
result := <-c
m[result.Key] = result
}
}

func Run(singleThreadFlag *bool, c chan Result, m map[string]Result) {
if !*singleThreadFlag {
*singleThreadFlag = true
go Channel2Map(c, m)
}
}
20 changes: 20 additions & 0 deletions routine/channel_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package routine

import (
"github.com/ssst0n3/awesome_libs/log"
"testing"
"time"
)

func TestRun(t *testing.T) {
var singleThreadFlag bool
chanResult := make(chan Result, 10)
mapResult := make(map[string]Result)
Run(&singleThreadFlag, chanResult, mapResult)
chanResult <- Result{
Key: "test",
Value: true,
}
time.Sleep(time.Millisecond*100)
log.Logger.Info(mapResult)
}

0 comments on commit 627c81c

Please sign in to comment.