@@ -53,5 +53,54 @@ def test_lock_file(self):
5353 lock_file ._obtain_lock_or_raise ()
5454 lock_file ._release_lock ()
5555
56+ def _cmp_contents (self , file_path , data ):
57+ # raise if data from file at file_path
58+ # does not match data string
59+ fp = open (file_path , "r" )
60+ try :
61+ assert fp .read () == data
62+ finally :
63+ fp .close ()
64+
5665 def test_safe_operation (self ):
57- self .fail ("todo" )
66+ my_file = tempfile .mktemp ()
67+ orig_data = "hello"
68+ new_data = "world"
69+ my_file_fp = open (my_file , "w" )
70+ my_file_fp .write (orig_data )
71+ my_file_fp .close ()
72+
73+ try :
74+ cwrite = ConcurrentWriteOperation (my_file )
75+
76+ # didn't start writing, doesnt matter
77+ cwrite ._end_writing (False )
78+ cwrite ._end_writing (True )
79+ assert not cwrite ._is_writing ()
80+
81+ # write data and fail
82+ stream = cwrite ._begin_writing ()
83+ assert cwrite ._is_writing ()
84+ stream .write (new_data )
85+ cwrite ._end_writing (successful = False )
86+ self ._cmp_contents (my_file , orig_data )
87+ assert not os .path .exists (stream .name )
88+
89+ # write data - concurrently
90+ ocwrite = ConcurrentWriteOperation (my_file )
91+ stream = cwrite ._begin_writing ()
92+ self .failUnlessRaises (IOError , ocwrite ._begin_writing )
93+
94+ stream .write ("world" )
95+ cwrite ._end_writing (successful = True )
96+ self ._cmp_contents (my_file , new_data )
97+ assert not os .path .exists (stream .name )
98+
99+ # could test automatic _end_writing on destruction
100+ finally :
101+ os .remove (my_file )
102+ # END final cleanup
103+
104+
105+
106+
0 commit comments