terminals/tests/Xtreams__PipeReadingWritingTest.st
changeset 9 6c90659cf105
child 26 d740fff525d4
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/terminals/tests/Xtreams__PipeReadingWritingTest.st	Mon Aug 22 16:04:00 2011 +0000
@@ -0,0 +1,65 @@
+"{ Package: 'stx:goodies/xtreams/terminals/tests' }"
+
+"{ NameSpace: Xtreams }"
+
+FiniteReadingWritingTests subclass:#PipeReadingWritingTest
+	instanceVariableNames:''
+	classVariableNames:''
+	poolDictionaries:'OS'
+	category:'Xtreams'
+!
+
+
+!PipeReadingWritingTest methodsFor:'initialize-release'!
+
+setUp
+	| pipe |
+	(OSHandle currentOS = #win32) ifTrue: [self error: 'Does not work on Windows' ].
+	pipe := OSSystemSupport concreteClass pipeAccessorClass openPair.
+	input := pipe first reading.
+	output := pipe last writing.
+!
+
+tearDown
+	input close.
+	output close.
+! !
+
+!PipeReadingWritingTest methodsFor:'utilities'!
+
+timeout: timeoutDuration server: serverBlock client: clientBlock
+	"For tests that must be able to write and read at the same time, where they may block on each.
+	 Sockets, Pipes have an operating system buffer which dictates its bandwidth."
+
+	|	timeout
+		serverSuccess serverProcess serverLock
+		clientSuccess clientProcess clientLock|
+
+	timeout := false.
+	serverLock := Semaphore new.
+	clientLock := Semaphore new.
+	serverSuccess := clientSuccess := false.
+	serverProcess := [serverSuccess := serverBlock value. serverLock signal] fork.
+	clientProcess := [clientSuccess := clientBlock value. clientLock signal] fork.
+
+	Core.Timer after: timeoutDuration do:
+		[serverProcess terminate.
+		output ifNotNil: [ output close ].
+		clientProcess terminate.
+		input ifNotNil: [ input close ].
+		timeout := true.
+		serverLock signal.
+		clientLock signal].
+	serverLock wait.
+	clientLock wait.
+
+	self assert: serverSuccess.
+	self assert: clientSuccess.
+	self deny: timeout
+! !
+
+!PipeReadingWritingTest class methodsFor:'documentation'!
+
+version_SVN
+    ^ '$Id$'
+! !