root/projects/sAsync/trunk/sasync/test/mock.py

Revision 57, 4.0 kB (checked in by edsuom, 1 year ago)

Updated sAsync to use asynqueue package instead of twisted_goodies.taskqueue; updated ez_setup.py scripts for all projects

Line 
1 # sAsync:
2 # An enhancement to the SQLAlchemy package that provides persistent
3 # dictionaries, text indexing and searching, and an access broker for
4 # conveniently managing database access, table setup, and
5 # transactions. Everything can be run in an asynchronous fashion using the
6 # Twisted framework and its deferred processing capabilities.
7 #
8 # Copyright (C) 2006-2007 by Edwin A. Suominen, http://www.eepatents.com
9 #
10 # This program is free software; you can redistribute it and/or modify it under
11 # the terms of the GNU General Public License as published by the Free Software
12 # Foundation; either version 2 of the License, or (at your option) any later
13 # version.
14 #
15 # This program is distributed in the hope that it will be useful, but WITHOUT
16 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 # FOR A PARTICULAR PURPOSE.  See the file COPYING for more details.
18 #
19 # You should have received a copy of the GNU General Public License along with
20 # this program; if not, write to the Free Software Foundation, Inc., 51
21 # Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22
23 """
24 Mocks objects
25 """
26
27 import random
28 import zope.interface
29 from twisted.internet import reactor, defer
30 from asynqueue import IWorker
31
32
33 VERBOSE = False
34
35
36 class MockTask(object):
37     def __init__(self, f, args, kw, priority, series):
38         self.ran = False
39         self.callTuple = (f, args, kw)
40         self.priority = priority
41         self.series = series
42         self.d = defer.Deferred()
43    
44     def __cmp__(self, other):
45         if other is None:
46             return -1
47         return cmp(self.priority, other.priority)
48
49     def __str__(self):
50         return str(self.callTuple[0])
51
52
53 class MockWorker(object):
54     zope.interface.implements(IWorker)
55
56     def __init__(self, runDelay=0.0):
57         self.runDelay = runDelay
58         self.ran = []
59         self.isShutdown = False
60
61     def run(self, task):
62         def ran(result, d):
63             d.callback(None)
64             return result
65        
66         self.task = task
67         reactor.callLater(self.runDelay, self._reallyRun)
68         d = defer.Deferred()
69         task.d.addCallback(ran, d)
70         return d
71    
72     def _reallyRun(self):
73         f, args, kw = self.task.callTuple
74         result = f(*args, **kw)
75         self.ran.append(self.task)
76         if VERBOSE:
77             ID = getattr(self, 'ID', 0)
78             print "Worker %d ran %s = %s" % (ID, str(self.task), result)
79         self.task.d.callback(result)
80
81     def stop(self):
82         self.isShutdown = True
83         if VERBOSE:
84             print "Shutting down worker %s" % self
85         d = getattr(getattr(self, 'task', None), 'd', None)
86         if d is None or d.called:
87             d_shutdown = defer.succeed(None)
88         else:
89             d_shutdown = defer.Deferred()
90             d.chainDeferred(d_shutdown)
91         return d_shutdown
92
93     def crash(self):
94         pass
95
96
97 class MockThing:
98     def __init__(self):
99         self.beenThereDoneThat = False
100    
101     def method(self, x):
102         self.beenThereDoneThat = True
103         return 2*x
104
105     def __cmp__(self, other):
106         if not hasattr(other, 'beenThereDoneThat'):
107             # We are superior; we have the attribute and 'other' doesn't!
108             return 1
109         elif self.beenThereDoneThat and not other.beenThereDoneThat:
110             return 1
111         elif not self.beenThereDoneThat and other.beenThereDoneThat:
112             return -1
113         else:
114             return 0
115
116
117 class MockSearch:
118     def __init__(self):
119         self.lock = defer.DeferredLock()
120         random.seed()
121    
122     def busy(self, *args):
123         pass
124
125     def ready(self, *args):
126         pass
127
128     def index(self, value, **kw):
129         def done(lock):
130             if VERBOSE:
131                 print "Indexed '%s'" % str(value)
132             lock.release()
133
134         def delay():
135             return random.uniform(0.0, 0.5)
136
137         def gotLock(lock):
138             reactor.callLater(delay(), done, lock)
139            
140         d = self.lock.acquire()
141         d.addCallback(gotLock)
142         return d
Note: See TracBrowser for help on using the browser.