src/JavaMonitorsTests.st
branchjk_new_structure
changeset 1146 e458dd16772e
parent 1145 8728f5373a48
child 1152 040cba55a7d2
--- a/src/JavaMonitorsTests.st	Tue Nov 22 10:12:20 2011 +0000
+++ b/src/JavaMonitorsTests.st	Tue Nov 22 12:04:31 2011 +0000
@@ -42,7 +42,7 @@
 "{ Package: 'stx:libjava' }"
 
 TestCase subclass:#JavaMonitorsTests
-	instanceVariableNames:'result reason thisProcess assertionAccess'
+	instanceVariableNames:'result reason thisProcess assertionAccess thisProcessAccess'
 	classVariableNames:''
 	poolDictionaries:''
 	category:'Languages-Java-Tests-Synchronization'
@@ -185,6 +185,7 @@
     reason := 'Everything went just fine'.
     thisProcess := Processor activeProcess.
     assertionAccess := Semaphore forMutualExclusion.
+    thisProcessAccess := Semaphore forMutualExclusion.
 
     "Created: / 20-11-2011 / 18:55:39 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
 !
@@ -202,8 +203,8 @@
 !
 
 waitForAndResumeThisProcess
-    self waitForStoppingThread: thisProcess.
-    thisProcess resume.
+    thisProcessAccess critical: [self waitForStoppingThread: thisProcess.
+    thisProcess resume.]
 
     "Created: / 20-11-2011 / 19:17:53 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
 !
@@ -300,12 +301,12 @@
     t := [
                 count timesRepeat: [ mon enter ].
                 count timesRepeat: [
-                    self assert: (mon isOwnedBy: t).
+                    self assert: (mon isOwnedBy: t) message: 'mon was not owned by t1'.
                     mon exit
                 ].
                 self waitForAndResumeThisProcess
             ] newProcess.
-            t resume.
+    t resume.
     self stop.
     self validateResult.
 
@@ -318,19 +319,17 @@
     t1 := [
                 mon enter.
                 mon enter.
-                self assert: (mon isOwnedBy: t1).
+                self assert: (mon isOwnedBy: t1) message: 'mon was not owned by t1 after multiple enter'.
                 mon exit.
                 t1 stop.
-                self assert: (mon isOwnedBy: t1).
-                
+                self assert: (mon isOwnedBy: t1) message:'mon was not owned by t1 after single exit'.
                 mon exit.
-                
                 self waitForDyingThread: t2.
                 self waitForAndResumeThisProcess
             ] newProcess.
     t2 := [
                 self waitForStoppingThread: t1.
-                self assert: (mon isOwnedBy: t1).
+                self assert: (mon isOwnedBy: t1) message: 'mon was not owned by t1 after stop'.
                 t1 resume.
             ] newProcess.
     t1 resume.
@@ -386,6 +385,137 @@
     "Created: / 20-11-2011 / 14:51:22 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
 ! !
 
+!JavaMonitorsTests methodsFor:'wait notify tests'!
+
+testManyThreadsWaitNotifyAll
+    | mon  threads |
+    mon := JavaMonitor new.
+    threads := OrderedCollection new.
+    2 timesRepeat: [
+        | t |
+        t := [
+                    mon enter.
+                    self assert: (mon isOwnedBy: t)
+                        message: 'thread was not owned by t after enter'.
+                    self waitForAndResumeThisProcess.
+                    'resumed thisProcess' infoPrintCR.
+                    mon wait.
+                    'notified and alive' infoPrintCR.
+                    self assert: (mon isOwnedBy: t)
+                        message: 'thread was not owned by t after wait'.
+                        mon exit.
+                    'dying' infoPrintCR.
+                ] newProcess.
+        threads add: t.
+    ].
+    threads do: [
+        :each | 
+        'resuming t' infoPrintCR.
+        each resume
+    ].
+    threads do: [
+        :each | 
+        'stopping' infoPrintCR.
+        self stop
+    ].
+    threads do: [
+        :each | 
+        'waiting for waiting' infoPrintCR.
+        self waitForWaitingThread: each
+    ].
+    'sync barrier reached' infoPrintCR.
+    mon enter.
+    mon notifyAll.
+    'threads has been notified' infoPrintCR.
+    mon exit.
+    'monitor exitted' infoPrintCR.
+    threads do: [
+        :each | 
+        'waiting for them to die' infoPrintCR.
+        self waitForDyingThread: each
+    ].
+    self validateResult.
+
+    "Created: / 22-11-2011 / 12:27:16 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
+!
+
+testOneThreadWaitMultipleEnters
+    | mon  t |
+    mon := JavaMonitor new.
+    t := [
+                mon enter.
+                mon enter.
+                mon enter.
+                self waitForAndResumeThisProcess.
+                self assert: (mon isOwnedBy: t)
+                    message: 'thread was not owned by t after enter'.
+                mon wait.
+                self assert: (mon isOwnedBy: t)
+                    message: 'thread was not owned by t after wait'.
+                mon exit.
+                self assert: (mon isOwnedBy: t)
+                    message: 'thread was not owned by t after wait'.
+                mon exit.
+                self assert: (mon isOwnedBy: t)
+                    message: 'thread was not owned by t after wait'.
+                mon exit.
+            ] newProcess.
+    t resume.
+    self stop.
+    mon enter.
+    mon notify.
+    mon exit.
+    self waitForDyingThread: t.
+    self validateResult.
+
+    "Created: / 22-11-2011 / 12:55:38 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
+!
+
+testOneThreadWaitNotify
+    | mon  t |
+    mon := JavaMonitor new.
+    t := [
+                mon enter.
+                self assert: (mon isOwnedBy: t)
+                    message: 'thread was not owned by t after enter'.
+                self waitForAndResumeThisProcess.
+                mon wait.
+                self assert: (mon isOwnedBy: t)
+                    message: 'thread was not owned by t after wait'.
+                self waitForAndResumeThisProcess
+            ] newProcess.
+    t resume.
+    self stop.
+    mon enter.
+    mon notify.
+    mon exit.
+    self stop.
+    self validateResult.
+
+    "Created: / 22-11-2011 / 11:51:28 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
+!
+
+testOneThreadWaitTimeout
+    | mon  t |
+    mon := JavaMonitor new.
+    t := [
+                mon enter.
+                self assert: (mon isOwnedBy: t)
+                    message: 'thread was not owned by t after enter'.
+                
+                "/notify will never come
+                
+                mon waitForMilliseconds: 500.
+                self assert: (mon isOwnedBy: t)
+                    message: 'thread was not owned by t after wait'.
+            ] newProcess.
+    t resume.
+    self waitForDyingThread: t.
+    self validateResult.
+
+    "Created: / 22-11-2011 / 12:51:13 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
+! !
+
 !JavaMonitorsTests class methodsFor:'documentation'!
 
 version_SVN