35
35
from dlg .apps .app_base import BarrierAppDROP
36
36
from dlg .data .drops .directorycontainer import DirectoryContainer
37
37
from dlg .data .drops .file import FileDROP
38
+ from dlg .data .drops .memory import InMemoryDROP
38
39
from dlg .droputils import DROPWaiterCtx
39
40
from dlg .lifecycle import dlm
40
41
@@ -61,7 +62,15 @@ def test_dropAddition(self):
61
62
62
63
def test_dropCompleteTriggersReplication (self ):
63
64
with dlm .DataLifecycleManager (enable_drop_replication = True ) as manager :
64
- drop = FileDROP ("oid:A" , "uid:A1" , expectedSize = 1 )
65
+
66
+ # By default a file is non-persistent
67
+ drop = FileDROP ("oid:B" , "uid:B1" , expectedSize = 1 )
68
+ manager .addDrop (drop )
69
+ self ._writeAndClose (drop )
70
+ self .assertEqual (DROPPhases .GAS , drop .phase )
71
+ self .assertEqual (1 , len (manager .getDropUids (drop )))
72
+
73
+ drop = FileDROP ("oid:A" , "uid:A1" , expectedSize = 1 , persist = True )
65
74
manager .addDrop (drop )
66
75
self ._writeAndClose (drop )
67
76
@@ -70,12 +79,6 @@ def test_dropCompleteTriggersReplication(self):
70
79
self .assertEqual (DROPPhases .SOLID , drop .phase )
71
80
self .assertEqual (2 , len (manager .getDropUids (drop )))
72
81
73
- # Try the same with a non-persisted data object, it shouldn't be replicated
74
- drop = FileDROP ("oid:B" , "uid:B1" , expectedSize = 1 , persist = False )
75
- manager .addDrop (drop )
76
- self ._writeAndClose (drop )
77
- self .assertEqual (DROPPhases .GAS , drop .phase )
78
- self .assertEqual (1 , len (manager .getDropUids (drop )))
79
82
80
83
def test_expiringNormalDrop (self ):
81
84
with dlm .DataLifecycleManager (check_period = 0.5 ) as manager :
@@ -130,55 +133,113 @@ def test_cleanupExpiredDrops(self):
130
133
self .assertEqual (DROPStates .DELETED , drop .status )
131
134
self .assertFalse (drop .exists ())
132
135
133
- def test_expireAfterUse (self ):
136
+ def test_expireAfterUseForFile (self ):
137
+ """
138
+ Test default and non-default behaviour for the expireAfterUse flag
139
+ for file drops.
140
+
141
+ Default: expireAfterUse=False, so the drop should still exist after it
142
+ has been consumed.
143
+ Non-default: expiredAfterUse=True, so the drop will be expired and
144
+ deleted after it is consumed.
145
+ """
146
+
147
+ class MyApp (BarrierAppDROP ):
148
+ def run (self ):
149
+ pass
150
+
151
+ with dlm .DataLifecycleManager (check_period = 0.5 , cleanup_period = 2 ) as manager :
152
+
153
+ # Check default
154
+ default_fp , default_name = tempfile .mkstemp ()
155
+ default = FileDROP (
156
+ "a" ,
157
+ "a" ,
158
+ filepath = default_name
159
+ )
160
+
161
+ expired_fp , expired_name = tempfile .mkstemp ()
162
+ expired = FileDROP (
163
+ "b" ,
164
+ "b" ,
165
+ filepath = expired_name ,
166
+ expireAfterUse = True # Remove the file after use
167
+ )
168
+ c = MyApp ("c" , "c" )
169
+ d = MyApp ("d" , "d" )
170
+ default .addConsumer (c )
171
+ default .addConsumer (d )
172
+ expired .addConsumer (c )
173
+ expired .addConsumer (d )
174
+
175
+ manager .addDrop (default )
176
+ manager .addDrop (expired )
177
+ manager .addDrop (expired )
178
+ manager .addDrop (c )
179
+
180
+ # Make sure all consumers are done
181
+ with DROPWaiterCtx (self , [c , d ], 1 ):
182
+ default .setCompleted ()
183
+ expired .setCompleted ()
184
+
185
+ # Both directories should be there, but after cleanup B's shouldn't
186
+ # be there anymore
187
+ self .assertTrue (default .exists ())
188
+ self .assertTrue (expired .exists ())
189
+ time .sleep (2.5 )
190
+ self .assertTrue (default .exists ())
191
+ self .assertFalse (expired .exists ())
192
+ default .delete ()
193
+
194
+ def test_expireAfterUseForMemory (self ):
134
195
"""
135
- Simple test for the expireAfterUse flag. Two DROPs are created with
136
- different values, and after they are used we check whether their data
137
- is still there or not
196
+ Default: expireAfterUse=True, so the drop should not exist after it
197
+ has been consumed.
198
+ Non-default: expiredAfterUse=False, so the drop will be not be expired
199
+ after it is consumed.
138
200
"""
139
201
140
202
class MyApp (BarrierAppDROP ):
141
203
def run (self ):
142
204
pass
143
205
144
206
with dlm .DataLifecycleManager (check_period = 0.5 , cleanup_period = 2 ) as manager :
145
- a = DirectoryContainer (
207
+
208
+ # Check default behaviour - deleted for memory drops
209
+ default = InMemoryDROP (
146
210
"a" ,
147
211
"a" ,
148
- persist = False ,
149
- expireAfterUse = False , # Marking persist as False should lead to expiry regardless of this flag
150
- dirname = tempfile .mkdtemp (),
151
212
)
152
- b_dirname = tempfile .mkdtemp ()
153
- b = DirectoryContainer (
213
+
214
+ # Non-default behaviour - memory is not deleted
215
+ non_expired = InMemoryDROP (
154
216
"b" ,
155
217
"b" ,
156
- persist = True , # Persist should have no effect on expireAfterUse
157
- expireAfterUse = False ,
158
- dirname = b_dirname ,
218
+ expireAfterUse = False
159
219
)
160
220
c = MyApp ("c" , "c" )
161
221
d = MyApp ("d" , "d" )
162
- a .addConsumer (c )
163
- a .addConsumer (d )
164
- b .addConsumer (c )
165
- b .addConsumer (d )
166
-
167
- manager .addDrop (a )
168
- manager .addDrop (b )
169
- manager .addDrop (b )
222
+ default .addConsumer (c )
223
+ default .addConsumer (d )
224
+ non_expired .addConsumer (c )
225
+ non_expired .addConsumer (d )
226
+
227
+ manager .addDrop (default )
228
+ manager .addDrop (non_expired )
229
+ manager .addDrop (non_expired )
170
230
manager .addDrop (c )
171
231
172
232
# Make sure all consumers are done
173
233
with DROPWaiterCtx (self , [c , d ], 1 ):
174
- a .setCompleted ()
175
- b .setCompleted ()
234
+ default .setCompleted ()
235
+ non_expired .setCompleted ()
176
236
177
- # Both directories should be there, but after cleanup A 's shouldn't
237
+ # Both directories should be there, but after cleanup B 's shouldn't
178
238
# be there anymore
179
- self .assertTrue (a .exists ())
180
- self .assertTrue (b .exists ())
239
+ self .assertTrue (default .exists ())
240
+ self .assertTrue (non_expired .exists ())
181
241
time .sleep (2.5 )
182
- self .assertFalse (a .exists ())
183
- self .assertTrue (b .exists ())
184
- b .delete ()
242
+ self .assertFalse (default .exists ())
243
+ self .assertTrue (non_expired .exists ())
244
+ non_expired .delete ()
245
+
0 commit comments