2121from google .cloud .storage ._experimental .asyncio .async_appendable_object_writer import (
2222 AsyncAppendableObjectWriter ,
2323)
24+ from google .cloud .storage ._experimental .asyncio .async_appendable_object_writer import (
25+ _MAX_CHUNK_SIZE_BYTES ,
26+ )
2427from google .cloud import _storage_v2
2528
2629
2932GENERATION = 123
3033WRITE_HANDLE = b"test-write-handle"
3134PERSISTED_SIZE = 456
35+ EIGHT_MIB = 8 * 1024 * 1024
3236
3337
3438@pytest .fixture
@@ -52,6 +56,7 @@ def test_init(mock_write_object_stream, mock_client):
5256 assert not writer ._is_stream_open
5357 assert writer .offset is None
5458 assert writer .persisted_size is None
59+ assert writer .bytes_appended_since_last_flush == 0
5560
5661 mock_write_object_stream .assert_called_once_with (
5762 client = mock_client ,
@@ -78,6 +83,7 @@ def test_init_with_optional_args(mock_write_object_stream, mock_client):
7883
7984 assert writer .generation == GENERATION
8085 assert writer .write_handle == WRITE_HANDLE
86+ assert writer .bytes_appended_since_last_flush == 0
8187
8288 mock_write_object_stream .assert_called_once_with (
8389 client = mock_client ,
@@ -88,6 +94,60 @@ def test_init_with_optional_args(mock_write_object_stream, mock_client):
8894 )
8995
9096
97+ @mock .patch (
98+ "google.cloud.storage._experimental.asyncio.async_appendable_object_writer._AsyncWriteObjectStream"
99+ )
100+ def test_init_with_writer_options (mock_write_object_stream , mock_client ):
101+ """Test the constructor with optional arguments."""
102+ writer = AsyncAppendableObjectWriter (
103+ mock_client ,
104+ BUCKET ,
105+ OBJECT ,
106+ writer_options = {"FLUSH_INTERVAL_BYTES" : EIGHT_MIB },
107+ )
108+
109+ assert writer .flush_interval == EIGHT_MIB
110+ assert writer .bytes_appended_since_last_flush == 0
111+
112+ mock_write_object_stream .assert_called_once_with (
113+ client = mock_client ,
114+ bucket_name = BUCKET ,
115+ object_name = OBJECT ,
116+ generation_number = None ,
117+ write_handle = None ,
118+ )
119+
120+
121+ @mock .patch (
122+ "google.cloud.storage._experimental.asyncio.async_appendable_object_writer._AsyncWriteObjectStream"
123+ )
124+ def test_init_with_flush_interval_less_than_chunk_size_raises_error (mock_client ):
125+ """Test that an OutOfRange error is raised if flush_interval is less than the chunk size."""
126+
127+ with pytest .raises (exceptions .OutOfRange ):
128+ AsyncAppendableObjectWriter (
129+ mock_client ,
130+ BUCKET ,
131+ OBJECT ,
132+ writer_options = {"FLUSH_INTERVAL_BYTES" : _MAX_CHUNK_SIZE_BYTES - 1 },
133+ )
134+
135+
136+ @mock .patch (
137+ "google.cloud.storage._experimental.asyncio.async_appendable_object_writer._AsyncWriteObjectStream"
138+ )
139+ def test_init_with_flush_interval_not_multiple_of_chunk_size_raises_error (mock_client ):
140+ """Test that an OutOfRange error is raised if flush_interval is not a multiple of the chunk size."""
141+
142+ with pytest .raises (exceptions .OutOfRange ):
143+ AsyncAppendableObjectWriter (
144+ mock_client ,
145+ BUCKET ,
146+ OBJECT ,
147+ writer_options = {"FLUSH_INTERVAL_BYTES" : _MAX_CHUNK_SIZE_BYTES + 1 },
148+ )
149+
150+
91151@mock .patch ("google.cloud.storage._experimental.asyncio._utils.google_crc32c" )
92152@mock .patch (
93153 "google.cloud.storage._experimental.asyncio.async_grpc_client.AsyncGrpcClient.grpc_client"
@@ -477,7 +537,7 @@ async def test_append_flushes_when_buffer_is_full(
477537):
478538 """Test that append flushes the stream when the buffer size is reached."""
479539 from google .cloud .storage ._experimental .asyncio .async_appendable_object_writer import (
480- _MAX_BUFFER_SIZE_BYTES ,
540+ _DEFAULT_FLUSH_INTERVAL_BYTES ,
481541 )
482542
483543 writer = AsyncAppendableObjectWriter (mock_client , BUCKET , OBJECT )
@@ -487,7 +547,7 @@ async def test_append_flushes_when_buffer_is_full(
487547 mock_stream .send = mock .AsyncMock ()
488548 writer .simple_flush = mock .AsyncMock ()
489549
490- data = b"a" * _MAX_BUFFER_SIZE_BYTES
550+ data = b"a" * _DEFAULT_FLUSH_INTERVAL_BYTES
491551 await writer .append (data )
492552
493553 writer .simple_flush .assert_awaited_once ()
@@ -500,7 +560,7 @@ async def test_append_flushes_when_buffer_is_full(
500560async def test_append_handles_large_data (mock_write_object_stream , mock_client ):
501561 """Test that append handles data larger than the buffer size."""
502562 from google .cloud .storage ._experimental .asyncio .async_appendable_object_writer import (
503- _MAX_BUFFER_SIZE_BYTES ,
563+ _DEFAULT_FLUSH_INTERVAL_BYTES ,
504564 )
505565
506566 writer = AsyncAppendableObjectWriter (mock_client , BUCKET , OBJECT )
@@ -510,7 +570,7 @@ async def test_append_handles_large_data(mock_write_object_stream, mock_client):
510570 mock_stream .send = mock .AsyncMock ()
511571 writer .simple_flush = mock .AsyncMock ()
512572
513- data = b"a" * (_MAX_BUFFER_SIZE_BYTES * 2 + 1 )
573+ data = b"a" * (_DEFAULT_FLUSH_INTERVAL_BYTES * 2 + 1 )
514574 await writer .append (data )
515575
516576 assert writer .simple_flush .await_count == 2
0 commit comments