def test_readFromBackingErrorHandling(self): client = Object vos_VOFILE = Object() vos_VOFILE.URLs = ["url0", "URL1"] vos_VOFILE.urlIndex = 0 vos_VOFILE.open = Mock() mock_resp1 = Mock() mock_resp1.iter_content = Mock(return_value="bar") mock_resp2 = Mock() mock_resp2.iter_content = Mock(return_value="foo") returns = [mock_resp1, mock_resp2] def side_effect(*args, **kwds): return returns.pop(0) vos_VOFILE.read = MagicMock(side_effect=side_effect) vos_VOFILE.close = Mock() client.open = Mock(return_value=vos_VOFILE) myVofs = Mock() myVofs.client = client testProxy = vofs.MyIOProxy(myVofs, None) testProxy.writeToCache = Mock() path = "/dir1/dir2/file" cacheFile = Object() cacheFile.path = path cacheFile.gotHeader = True cacheFile.cache = Object() testProxy.setCacheFile(cacheFile) testProxy.readFromBacking()
def test_getSize(self): testProxy = vofs.MyIOProxy(None, "vos:/anode") node = vos.Node( "vos:/anode", properties={"MD5": "1234", "length": "1"}) testProxy.vofs = Object() testProxy.vofs.get_node = Mock(side_effect=SideEffect({ ('vos:/anode',): node, }, name="testfs.getNode")) self.assertEqual(testProxy.getSize(), 1) testProxy.size = 27 self.assertEqual(testProxy.getSize(), 27)
def testWriteToBacking(self): # Submit a write request for the whole file. with Cache(TestVOFS.testCacheDir, 100, timeout=1) as testCache: client = Object client.copy = Mock() client.copy = Mock(return_value=12345) vofsObj = Mock() vofsObj.client = client node = Object node.uri = "vos:/dir1/dir2/file" node.props = {"MD5": 12345} vofsObj.get_node = Mock(return_value=node) testProxy = vofs.MyIOProxy(vofsObj, None) path = "/dir1/dir2/file" with FileHandle(path, testCache, testProxy) as testFileHandle: testProxy.cacheFile = testFileHandle self.assertEqual(testProxy.writeToBacking(), 12345) client.copy.assert_called_once_with( testCache.dataDir + "/dir1/dir2/file", node.uri, send_md5=True)
def testReadFromBacking(self): callCount = [0] def mock_read(block_size): callCount[0] += 1 if callCount[0] == 1: return "1234" else: return None with Cache(TestVOFS.testCacheDir, 100, timeout=1) as testCache: client = Object vos_VOFILE = Object() vos_VOFILE.URLs = ["url0", "URL1"] vos_VOFILE.urlIndex = 0 vos_VOFILE.open = Mock() vos_VOFILE.read = Mock(side_effect=mock_read) vos_VOFILE.close = Mock() client.open = Mock(return_value=vos_VOFILE) path = "/dir1/dir2/file" myVofs = Mock() myVofs.cacheFile = Mock() myVofs.cacheFile.path = path myVofs.client = client client.getFileInfo = Mock(return_value=(123, "456", 45)) testProxy = vofs.MyIOProxy(myVofs, None) with FileHandle(path, testCache, testProxy) as \ testFileHandle: testProxy.writeToCache = Mock(return_value=4) testProxy.cacheFile = testFileHandle testProxy.cacheFile.readThread = Mock() testProxy.cacheFile.readThread.aborted = False try: # Submit a request for the whole file testProxy.readFromBacking() client.open.assert_called_once_with( path, mode=os.O_RDONLY, view="data", size=None, range=None) self.assertEqual(vos_VOFILE.close.call_count, 1) self.assertEqual(vos_VOFILE.read.call_count, 2) # Submit a range request vos_VOFILE.close.reset_mock() vos_VOFILE.read.reset_mock() callCount[0] = 0 testProxy.readFromBacking(100, 200) self.assertEqual(client.open.call_count, 1) vos_VOFILE.open.assert_called_once_with( "url0", bytes="bytes=200-299") self.assertEqual(vos_VOFILE.close.call_count, 1) self.assertEqual(vos_VOFILE.read.call_count, 2) # Submit a request which gets aborted. vos_VOFILE.open.reset_mock() vos_VOFILE.close.reset_mock() vos_VOFILE.read.reset_mock() callCount[0] = 0 testProxy.writeToCache.side_effect = CacheAborted( "aborted") testProxy.readFromBacking(150, 200) self.assertEqual(client.open.call_count, 1) vos_VOFILE.open.assert_called_once_with( "url0", bytes="bytes=200-349") self.assertEqual(vos_VOFILE.close.call_count, 1) self.assertEqual(vos_VOFILE.read.call_count, 1) # Submit a request with size = None and offset > 0 vos_VOFILE.open.reset_mock() vos_VOFILE.close.reset_mock() vos_VOFILE.read.reset_mock() callCount[0] = 0 testProxy.readFromBacking(None, 1) self.assertEqual(client.open.call_count, 1) vos_VOFILE.open.assert_called_once_with("url0", bytes="bytes=1-") self.assertEqual(vos_VOFILE.close.call_count, 1) self.assertEqual(vos_VOFILE.read.call_count, 1) # Do a read which fails. This should result in a # renegotiation. vos_VOFILE.open.reset_mock() vos_VOFILE.close.reset_mock() vos_VOFILE.read.reset_mock() callCount[0] = 0 self.assertTrue(testProxy.lastVOFile is not None) testProxy.lastVOFile.read = Mock(side_effect=OSError) # This throws an exception because read will be called # twice, the first is caught and error handling occurs, the # second is not caught. with self.assertRaises(OSError): testProxy.readFromBacking(None, 1) self.assertEqual(client.open.call_count, 2) vos_VOFILE.open.assert_called_with("url0", bytes="bytes=1-") self.assertEqual(vos_VOFILE.close.call_count, 1) self.assertEqual(vos_VOFILE.read.call_count, 2) self.assertTrue(type(testProxy.exception) is OSError) testProxy.exception = None except Exception as e: print("unexpected exception", e) finally: testProxy.cacheFile.readThread = None