| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -199,10 +199,28 @@ async def append(self, data: bytes) -> None: | |||
| 199 | 199 | self.offset += chunk_size | |
| 200 | 200 | bytes_to_flush += chunk_size | |
| 201 | 201 | if bytes_to_flush >= _MAX_BUFFER_SIZE_BYTES: | |
| 202 | - await self.flush() | ||
| 202 | + await self.simple_flush() | ||
| 203 | 203 | bytes_to_flush = 0 | |
| 204 | 204 | start_idx = end_idx | |
| 205 | 205 | ||
| 206 | + async def simple_flush(self) -> None: | ||
| 207 | + """Flushes the data to the server. | ||
| 208 | + Please note: Unlike `flush` it does not do `state_lookup` | ||
| 209 | + | ||
| 210 | + :rtype: None | ||
| 211 | + | ||
| 212 | + :raises ValueError: If the stream is not open (i.e., `open()` has not | ||
| 213 | + been called). | ||
| 214 | + """ | ||
| 215 | + if not self._is_stream_open: | ||
| 216 | + raise ValueError("Stream is not open. Call open() before simple_flush().") | ||
| 217 | + | ||
| 218 | + await self.write_obj_stream.send( | ||
| 219 | + _storage_v2.BidiWriteObjectRequest( | ||
| 220 | + flush=True, | ||
| 221 | + ) | ||
| 222 | + ) | ||
| 223 | + | ||
| 206 | 224 | async def flush(self) -> int: | |
| 207 | 225 | """Flushes the data to the server. | |
| 208 | 226 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -224,6 +224,38 @@ async def test_flush_without_open_raises_value_error(mock_client): | |||
| 224 | 224 | await writer.flush() | |
| 225 | 225 | ||
| 226 | 226 | ||
| 227 | + @pytest.mark.asyncio | ||
| 228 | + @mock.patch( | ||
| 229 | + "google.cloud.storage._experimental.asyncio.async_appendable_object_writer._AsyncWriteObjectStream" | ||
| 230 | + ) | ||
| 231 | + async def test_simple_flush(mock_write_object_stream, mock_client): | ||
| 232 | + """Test that flush sends the correct request and updates state.""" | ||
| 233 | + # Arrange | ||
| 234 | + writer = AsyncAppendableObjectWriter(mock_client, BUCKET, OBJECT) | ||
| 235 | + writer._is_stream_open = True | ||
| 236 | + mock_stream = mock_write_object_stream.return_value | ||
| 237 | + mock_stream.send = mock.AsyncMock() | ||
| 238 | + | ||
| 239 | + # Act | ||
| 240 | + await writer.simple_flush() | ||
| 241 | + | ||
| 242 | + # Assert | ||
| 243 | + mock_stream.send.assert_awaited_once_with( | ||
| 244 | + _storage_v2.BidiWriteObjectRequest(flush=True) | ||
| 245 | + ) | ||
| 246 | + | ||
| 247 | + | ||
| 248 | + @pytest.mark.asyncio | ||
| 249 | + async def test_simple_flush_without_open_raises_value_error(mock_client): | ||
| 250 | + """Test that flush raises an error if the stream is not open.""" | ||
| 251 | + writer = AsyncAppendableObjectWriter(mock_client, BUCKET, OBJECT) | ||
| 252 | + with pytest.raises( | ||
| 253 | + ValueError, | ||
| 254 | + match="Stream is not open. Call open\\(\\) before simple_flush\\(\\).", | ||
| 255 | + ): | ||
| 256 | + await writer.simple_flush() | ||
| 257 | + | ||
| 258 | + | ||
| 227 | 259 | @pytest.mark.asyncio | |
| 228 | 260 | @mock.patch( | |
| 229 | 261 | "google.cloud.storage._experimental.asyncio.async_appendable_object_writer._AsyncWriteObjectStream" | |
@@ -369,7 +401,7 @@ async def test_append_sends_data_in_chunks(mock_write_object_stream, mock_client | |||
| 369 | 401 | writer.persisted_size = 100 | |
| 370 | 402 | mock_stream = mock_write_object_stream.return_value | |
| 371 | 403 | mock_stream.send = mock.AsyncMock() | |
| 372 | - writer.flush = mock.AsyncMock() | ||
| 404 | + writer.simple_flush = mock.AsyncMock() | ||
| 373 | 405 | ||
| 374 | 406 | data = b"a" * (_MAX_CHUNK_SIZE_BYTES + 1) | |
| 375 | 407 | await writer.append(data) | |
@@ -387,7 +419,7 @@ async def test_append_sends_data_in_chunks(mock_write_object_stream, mock_client | |||
| 387 | 419 | assert len(second_call[0][0].checksummed_data.content) == 1 | |
| 388 | 420 | ||
| 389 | 421 | assert writer.offset == 100 + len(data) | |
| 390 | - writer.flush.assert_not_awaited() | ||
| 422 | + writer.simple_flush.assert_not_awaited() | ||
| 391 | 423 | ||
| 392 | 424 | ||
| 393 | 425 | @pytest.mark.asyncio | |
@@ -407,12 +439,12 @@ async def test_append_flushes_when_buffer_is_full( | |||
| 407 | 439 | writer.persisted_size = 0 | |
| 408 | 440 | mock_stream = mock_write_object_stream.return_value | |
| 409 | 441 | mock_stream.send = mock.AsyncMock() | |
| 410 | - writer.flush = mock.AsyncMock() | ||
| 442 | + writer.simple_flush = mock.AsyncMock() | ||
| 411 | 443 | ||
| 412 | 444 | data = b"a" * _MAX_BUFFER_SIZE_BYTES | |
| 413 | 445 | await writer.append(data) | |
| 414 | 446 | ||
| 415 | - writer.flush.assert_awaited_once() | ||
| 447 | + writer.simple_flush.assert_awaited_once() | ||
| 416 | 448 | ||
| 417 | 449 | ||
| 418 | 450 | @pytest.mark.asyncio | |
@@ -430,12 +462,12 @@ async def test_append_handles_large_data(mock_write_object_stream, mock_client): | |||
| 430 | 462 | writer.persisted_size = 0 | |
| 431 | 463 | mock_stream = mock_write_object_stream.return_value | |
| 432 | 464 | mock_stream.send = mock.AsyncMock() | |
| 433 | - writer.flush = mock.AsyncMock() | ||
| 465 | + writer.simple_flush = mock.AsyncMock() | ||
| 434 | 466 | ||
| 435 | 467 | data = b"a" * (_MAX_BUFFER_SIZE_BYTES * 2 + 1) | |
| 436 | 468 | await writer.append(data) | |
| 437 | 469 | ||
| 438 | - assert writer.flush.await_count == 2 | ||
| 470 | + assert writer.simple_flush.await_count == 2 | ||
| 439 | 471 | ||
| 440 | 472 | ||
| 441 | 473 | @pytest.mark.asyncio | |
@@ -453,7 +485,7 @@ async def test_append_data_two_times(mock_write_object_stream, mock_client): | |||
| 453 | 485 | writer.persisted_size = 0 | |
| 454 | 486 | mock_stream = mock_write_object_stream.return_value | |
| 455 | 487 | mock_stream.send = mock.AsyncMock() | |
| 456 | - writer.flush = mock.AsyncMock() | ||
| 488 | + writer.simple_flush = mock.AsyncMock() | ||
| 457 | 489 | ||
| 458 | 490 | data1 = b"a" * (_MAX_CHUNK_SIZE_BYTES + 10) | |
| 459 | 491 | await writer.append(data1) | |
@@ -463,4 +495,4 @@ async def test_append_data_two_times(mock_write_object_stream, mock_client): | |||
| 463 | 495 | ||
| 464 | 496 | total_data_length = len(data1) + len(data2) | |
| 465 | 497 | assert writer.offset == total_data_length | |
| 466 | - assert writer.flush.await_count == 0 | ||
| 498 | + assert writer.simple_flush.await_count == 0 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments