From d4a77acb71658f379173a9c0e5c1b072da700e88 Mon Sep 17 00:00:00 2001 From: Stephon Striplin Date: Thu, 25 Aug 2011 14:32:45 -0700 Subject: [PATCH] Add atomic read tests --- s3tests/functional/test_s3.py | 63 +++++++++++++++++++++++++++++------ 1 file changed, 53 insertions(+), 10 deletions(-) diff --git a/s3tests/functional/test_s3.py b/s3tests/functional/test_s3.py index 010eb1a..d88d090 100644 --- a/s3tests/functional/test_s3.py +++ b/s3tests/functional/test_s3.py @@ -2088,6 +2088,7 @@ class FakeFile(object): def tell(self): return self.offset +class FakeWriteFile(FakeFile): def read(self, size=-1): if size < 0: size = self.size - self.offset @@ -2100,6 +2101,54 @@ class FakeFile(object): return self.char*count +class FakeReadFile(FakeFile): + def __init__(self, size, char='A', interrupt=None): + FakeFile.__init__(self, size, char, interrupt) + self.interrupted = False + + def write(self, chars): + eq(chars, self.char*len(chars)) + self.offset += len(chars) + + # Sneaky! do stuff on the second seek + if not self.interrupted and self.interrupt != None \ + and self.offset > 0: + self.interrupt() + self.interrupted = True + +def _verify_atomic_key_data(key, size=-1, char=None): + fp_verify = FakeFileVerifier(char) + key.get_contents_to_file(fp_verify) + if size >= 0: + eq(fp_verify.size, size) + +def _test_atomic_read(file_size): + bucket = get_new_bucket() + key = bucket.new_key('testobj') + + # create object of As + fp_a = FakeWriteFile(file_size, 'A') + key.set_contents_from_file(fp_a) + + fp_b = FakeWriteFile(file_size, 'B') + fp_a2 = FakeReadFile(file_size, 'A', + lambda: key.set_contents_from_file(fp_b) + ) + + # read object while writing it to it + key.get_contents_to_file(fp_a2) + + _verify_atomic_key_data(key, file_size, 'B') + +def test_atomic_read_1mb(): + _test_atomic_read(1024*1024) + +def test_atomic_read_4mb(): + _test_atomic_read(1024*1024*4) + +def test_atomic_read_8mb(): + _test_atomic_read(1024*1024*8) + class FakeFileVerifier(object): def __init__(self, char=None): self.char = char @@ -2112,19 +2161,13 @@ class FakeFileVerifier(object): self.size += size eq(data, self.char*size) -def _verify_atomic_key_data(key, size=-1, char=None): - fp_verify = FakeFileVerifier(char) - key.get_contents_to_file(fp_verify) - if size >= 0: - eq(fp_verify.size, size) - def _test_atomic_write(file_size): bucket = get_new_bucket() objname = 'testobj' key = bucket.new_key(objname) # create file of A's - fp_a = FakeFile(file_size, 'A') + fp_a = FakeWriteFile(file_size, 'A') key.set_contents_from_file(fp_a) # verify A's @@ -2132,7 +2175,7 @@ def _test_atomic_write(file_size): # create file of B's # but try to verify the file before we finish writing all the B's - fp_b = FakeFile(file_size, 'B', + fp_b = FakeWriteFile(file_size, 'B', lambda: _verify_atomic_key_data(key, file_size) ) key.set_contents_from_file(fp_b) @@ -2160,8 +2203,8 @@ def _test_atomic_dual_write(file_size): # write file of B's # but before we're done, try to write all A's - fp_a = FakeFile(file_size, 'A') - fp_b = FakeFile(file_size, 'B', + fp_a = FakeWriteFile(file_size, 'A') + fp_b = FakeWriteFile(file_size, 'B', lambda: key2.set_contents_from_file(fp_a) ) key.set_contents_from_file(fp_b)