Commit 3ca730c8 authored by Jeremy Hylton's avatar Jeremy Hylton

Modify tests so they run more quickly.

Modify tests so that thread execution is actually interleaved; the old
version ended up having one thread run to completion, then the next, etc.

Use explicit counter to keep track of child threads, rather than
counting on long sleep to be long enough.
parent 128358b9
from Sync import Synchronized
import thread
from random import random
from time import sleep
class P(Synchronized):
def __init__(self,*args,**kw):
self.count=0
def inc(self):
c=self.count
sleep(random())
self.count=self.count+1
return c,self.count
c = self.count
if random() > 0.7:
sleep(1)
self.count = self.count + 1
return c, self.count
def incn(self,n):
c=self.count
for i in range(n): self.inc()
return c,self.count
c = self.count
for i in range(n):
self.inc()
return c, self.count
p=P(1,2,spam=3)
p = P(1, 2, spam=3)
def test():
for i in range(10):
n=3
old,new=p.incn(n)
print old,new
if old+n != new: print 'oops'
for i in range(10): thread.start_new_thread(test,())
sleep(50)
for i in range(8):
n = 3
old, new = p.incn(n)
if old + n != new:
print 'oops'
sleep(2)
thread_finished()
def thread_finished(lock=thread.allocate_lock()):
global num_threads
lock.acquire()
num_threads = num_threads - 1
lock.release()
num_threads = 8
for i in range(num_threads):
thread.start_new_thread(test, ())
while num_threads > 0:
sleep(1)
import ThreadLock, thread
from random import random
from time import sleep
......@@ -30,7 +29,8 @@ class P(Base):
def inc(self):
c=self.count
sleep(random())
if random() > 0.7:
sleep(1)
self.count=self.count+1
return c,self.count
......@@ -43,12 +43,24 @@ p=P(1,2,spam=3)
def test():
for i in range(10):
n=3
old,new=p.incn(n)
print old,new
if old+n != new: print 'oops'
for i in range(8):
n = 3
old, new = p.incn(n)
if old + n != new:
print 'oops'
sleep(3)
thread_finished()
def thread_finished(lock=thread.allocate_lock()):
global num_threads
lock.acquire()
num_threads = num_threads - 1
lock.release()
num_threads = 8
for i in range(num_threads):
thread.start_new_thread(test, ())
for i in range(10): thread.start_new_thread(test,())
sleep(50)
while num_threads > 0:
sleep(1)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment