-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
新文件: routine/channel_test.go
- Loading branch information
Showing
2 changed files
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |