Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MemoryTape no longer maintains a read position so it is thread safe. F…
…ixes #57
- Loading branch information
1 parent
55bd747
commit 467c884
Showing
9 changed files
with
94 additions
and
69 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
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
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
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
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
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
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
59 changes: 59 additions & 0 deletions
59
src/test/groovy/co/freeside/betamax/tape/MultiThreadedTapeAccessSpec.groovy
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,59 @@ | |||
package co.freeside.betamax.tape | |||
|
|||
import co.freeside.betamax.Tape | |||
import co.freeside.betamax.proxy.Request | |||
import co.freeside.betamax.util.message.BasicRequest | |||
import co.freeside.betamax.util.message.BasicResponse | |||
import spock.lang.Issue | |||
import spock.lang.Shared | |||
import spock.lang.Specification | |||
|
|||
import java.util.concurrent.CountDownLatch | |||
|
|||
import static java.util.concurrent.TimeUnit.SECONDS | |||
|
|||
@Issue('https://github.com/robfletcher/betamax/issues/57') | |||
class MultiThreadedTapeAccessSpec extends Specification { | |||
|
|||
@Shared Tape tape = new MemoryTape(name: 'multi_threaded_tape_access_spec') | |||
|
|||
void 'the correct response is replayed to each thread'() { | |||
given: 'a number of requests' | |||
List<Request> requests = (0..<threads).collect { i -> | |||
def request = new BasicRequest('GET', "http://example.com/$i") | |||
request.addHeader('X-Thread', i.toString()) | |||
request | |||
} | |||
println requests | |||
|
|||
and: 'some existing responses on tape' | |||
requests.eachWithIndex { request, i -> | |||
def response = new BasicResponse(status: 200, reason: 'OK', body: i.toString()) | |||
tape.record(request, response) | |||
} | |||
|
|||
when: 'requests are replayed concurrently' | |||
def finished = new CountDownLatch(threads) | |||
def responses = [:] | |||
requests.eachWithIndex { request, i -> | |||
Thread.start { | |||
def response = new BasicResponse() | |||
tape.play(request, response) | |||
responses[requests[i].getHeader('X-Thread')] = response.bodyAsText.text | |||
finished.countDown() | |||
} | |||
} | |||
|
|||
then: 'all threads complete' | |||
finished.await(1, SECONDS) | |||
|
|||
and: 'the correct response is returned to each request' | |||
responses.every { key, value -> | |||
key == value | |||
} | |||
|
|||
where: | |||
threads = 10 | |||
} | |||
|
|||
} |
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