Move ChannelReceivesSomething to tests package.
ChannelReceivesSomething (formerly WaitForChannel) originally was located in the helpers package. This move was done to remove a cyclic dependency with the nomand package.
This commit is contained in:
@ -310,20 +310,20 @@ func TestRawToCodeOceanWriter(t *testing.T) {
|
|||||||
func TestCodeOceanToRawReaderReturnsOnlyAfterOneByteWasRead(t *testing.T) {
|
func TestCodeOceanToRawReaderReturnsOnlyAfterOneByteWasRead(t *testing.T) {
|
||||||
reader := newCodeOceanToRawReader(nil)
|
reader := newCodeOceanToRawReader(nil)
|
||||||
|
|
||||||
read := make(chan int)
|
read := make(chan bool)
|
||||||
go func() {
|
go func() {
|
||||||
p := make([]byte, 10)
|
p := make([]byte, 10)
|
||||||
n, _ := reader.Read(p)
|
_, _ = reader.Read(p)
|
||||||
read <- n
|
read <- true
|
||||||
}()
|
}()
|
||||||
|
|
||||||
t.Run("Does not return immediately when there is no data", func(t *testing.T) {
|
t.Run("Does not return immediately when there is no data", func(t *testing.T) {
|
||||||
assert.False(t, waitForChannel(read, tests.ShortTimeout))
|
assert.False(t, tests.ChannelReceivesSomething(read, tests.ShortTimeout))
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Returns when there is data available", func(t *testing.T) {
|
t.Run("Returns when there is data available", func(t *testing.T) {
|
||||||
reader.buffer <- byte(42)
|
reader.buffer <- byte(42)
|
||||||
assert.True(t, waitForChannel(read, tests.ShortTimeout))
|
assert.True(t, tests.ChannelReceivesSomething(read, tests.ShortTimeout))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -343,34 +343,25 @@ func TestCodeOceanToRawReaderReturnsOnlyAfterOneByteWasReadFromConnection(t *tes
|
|||||||
cancel := reader.readInputLoop()
|
cancel := reader.readInputLoop()
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
read := make(chan int)
|
read := make(chan bool)
|
||||||
message := make([]byte, 10)
|
message := make([]byte, 10)
|
||||||
go func() {
|
go func() {
|
||||||
n, _ := reader.Read(message)
|
_, _ = reader.Read(message)
|
||||||
read <- n
|
read <- true
|
||||||
}()
|
}()
|
||||||
|
|
||||||
t.Run("Does not return immediately when there is no data", func(t *testing.T) {
|
t.Run("Does not return immediately when there is no data", func(t *testing.T) {
|
||||||
assert.False(t, waitForChannel(read, tests.ShortTimeout))
|
assert.False(t, tests.ChannelReceivesSomething(read, tests.ShortTimeout))
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Returns when there is data available", func(t *testing.T) {
|
t.Run("Returns when there is data available", func(t *testing.T) {
|
||||||
messages <- strings.NewReader("Hello")
|
messages <- strings.NewReader("Hello")
|
||||||
assert.True(t, waitForChannel(read, tests.ShortTimeout))
|
assert.True(t, tests.ChannelReceivesSomething(read, tests.ShortTimeout))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- Test suite specific test helpers ---
|
// --- Test suite specific test helpers ---
|
||||||
|
|
||||||
func waitForChannel(ch chan int, duration time.Duration) bool {
|
|
||||||
select {
|
|
||||||
case <-ch:
|
|
||||||
return true
|
|
||||||
case <-time.After(duration):
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func newNomadAllocationWithMockedApiClient(runnerId string) (r runner.Runner, mock *nomad.ExecutorAPIMock) {
|
func newNomadAllocationWithMockedApiClient(runnerId string) (r runner.Runner, mock *nomad.ExecutorAPIMock) {
|
||||||
mock = &nomad.ExecutorAPIMock{}
|
mock = &nomad.ExecutorAPIMock{}
|
||||||
r = runner.NewNomadJob(runnerId, mock)
|
r = runner.NewNomadJob(runnerId, mock)
|
||||||
|
@ -759,10 +759,5 @@ func TestNullReaderDoesNotReturnImmediately(t *testing.T) {
|
|||||||
_, _ = reader.Read(p)
|
_, _ = reader.Read(p)
|
||||||
close(readerReturned)
|
close(readerReturned)
|
||||||
}()
|
}()
|
||||||
|
assert.False(t, tests.ChannelReceivesSomething(readerReturned, tests.ShortTimeout))
|
||||||
select {
|
|
||||||
case <-readerReturned:
|
|
||||||
t.Fatal("Read should not return immediately.")
|
|
||||||
case <-time.After(tests.ShortTimeout):
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
14
tests/util.go
Normal file
14
tests/util.go
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
package tests
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
// ChannelReceivesSomething waits timeout seconds for something to be received from channel ch.
|
||||||
|
// If something is received, it returns true. If the timeout expires without receiving anything, it return false.
|
||||||
|
func ChannelReceivesSomething(ch chan bool, timeout time.Duration) bool {
|
||||||
|
select {
|
||||||
|
case <-ch:
|
||||||
|
return true
|
||||||
|
case <-time.After(timeout):
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user