From f49694d6b66ead2b6420a3f5f2b53435abd827e5 Mon Sep 17 00:00:00 2001 From: kevin Date: Fri, 2 Oct 2020 22:41:25 +0800 Subject: [PATCH] fix data race --- core/cmdline/input_test.go | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/core/cmdline/input_test.go b/core/cmdline/input_test.go index 9b815906..0a390a6e 100644 --- a/core/cmdline/input_test.go +++ b/core/cmdline/input_test.go @@ -14,22 +14,24 @@ import ( func TestEnterToContinue(t *testing.T) { r, w, err := os.Pipe() assert.Nil(t, err) + ow := os.Stdout + os.Stdout = w + or := os.Stdin + os.Stdin = r + defer func() { + os.Stdin = or + os.Stdout = ow + }() var wg sync.WaitGroup wg.Add(2) go func() { defer wg.Done() - ow := os.Stdout - os.Stdout = w fmt.Println() - os.Stdout = ow }() go func() { defer wg.Done() - or := os.Stdin - os.Stdin = r EnterToContinue() - os.Stdin = or }() wait := make(chan lang.PlaceholderType) @@ -48,24 +50,26 @@ func TestEnterToContinue(t *testing.T) { func TestReadLine(t *testing.T) { r, w, err := os.Pipe() assert.Nil(t, err) + ow := os.Stdout + os.Stdout = w + or := os.Stdin + os.Stdin = r + defer func() { + os.Stdin = or + os.Stdout = ow + }() const message = "hello" var wg sync.WaitGroup wg.Add(2) go func() { defer wg.Done() - ow := os.Stdout - os.Stdout = w fmt.Println(message) - os.Stdout = ow }() go func() { defer wg.Done() - or := os.Stdin - os.Stdin = r input := ReadLine("") assert.Equal(t, message, input) - os.Stdin = or }() wait := make(chan lang.PlaceholderType)