cameronlee314 commented on a change in pull request #1239: SAMZA-2421: Add SystemProducer for Azure Blob Storage URL: https://github.com/apache/samza/pull/1239#discussion_r360995982
########## File path: samza-azure/src/test/java/org/apache/samza/system/azureblob/avro/TestAzureBlobAvroWriter.java ########## @@ -0,0 +1,452 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.samza.system.azureblob.avro; + +import com.azure.storage.blob.BlobAsyncClient; +import com.azure.storage.blob.BlobContainerAsyncClient; +import com.azure.storage.blob.specialized.BlockBlobAsyncClient; +import org.apache.samza.system.azureblob.producer.AzureBlobWriterMetrics; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.LinkedBlockingDeque; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; +import org.apache.avro.Schema; +import org.apache.avro.file.DataFileWriter; +import org.apache.avro.generic.GenericDatumWriter; +import org.apache.avro.generic.GenericRecord; +import org.apache.avro.generic.IndexedRecord; +import org.apache.avro.io.BinaryEncoder; +import org.apache.avro.io.DatumWriter; +import org.apache.avro.io.EncoderFactory; +import org.apache.avro.specific.SpecificDatumWriter; +import org.apache.avro.specific.SpecificRecord; +import org.apache.samza.SamzaException; +import org.apache.samza.system.OutgoingMessageEnvelope; +import org.apache.samza.system.SystemStream; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import static org.mockito.Matchers.*; +import static org.mockito.Mockito.*; + + +@RunWith(PowerMockRunner.class) +@PrepareForTest({BlobContainerAsyncClient.class, BlockBlobAsyncClient.class, AzureBlobAvroWriter.class, AzureBlobOutputStream.class}) +public class TestAzureBlobAvroWriter { + private ThreadPoolExecutor threadPool; + private OutgoingMessageEnvelope ome; + private byte[] encodedRecord; + private AzureBlobAvroWriter azureBlobAvroWriter; + private DataFileWriter mockDataFileWriter; + private AzureBlobOutputStream mockAzureBlobOutputStream; + private BlockBlobAsyncClient mockBlockBlobAsyncClient; + + private static final String VALUE = "FAKE_VALUE"; + private static final String SYSTEM_NAME = "FAKE_SYSTEM"; + private static final int THRESHOLD = 100; + + private class SpecificRecordEvent extends org.apache.avro.specific.SpecificRecordBase + implements org.apache.avro.specific.SpecificRecord { + public final org.apache.avro.Schema schema = org.apache.avro.Schema.parse( + "{\"type\":\"record\",\"name\":\"SpecificRecordEvent\",\"namespace\":\"org.apache.samza.events\",\"fields\":[]}"); + + public org.apache.avro.Schema getSchema() { + return schema; + } + + public java.lang.Object get(int field) { + return null; + } + + public void put(int field, Object value) {} + } + + private class GenericRecordEvent implements org.apache.avro.generic.GenericRecord { + public final org.apache.avro.Schema schema = org.apache.avro.Schema.parse( + "{\"type\":\"record\",\"name\":\"GenericRecordEvent\",\"namespace\":\"org.apache.samza.events\",\"fields\":[]}"); + + public org.apache.avro.Schema getSchema() { + return schema; + } + + public java.lang.Object get(String key) { + return null; + } + + public java.lang.Object get(int field) { + return null; + } + + public void put(int field, Object value) {} + public void put(String key, Object value) {} + } + + private OutgoingMessageEnvelope createOME(String streamName) { + SystemStream systemStream = new SystemStream(SYSTEM_NAME, streamName); + SpecificRecord record = new SpecificRecordEvent(); + return new OutgoingMessageEnvelope(systemStream, record); + } + + private OutgoingMessageEnvelope createOMEGenericRecord(String streamName) { + SystemStream systemStream = new SystemStream(SYSTEM_NAME, streamName); + GenericRecord record = new GenericRecordEvent(); + return new OutgoingMessageEnvelope(systemStream, record); + } + + @Before + public void setup() throws Exception { + threadPool = new ThreadPoolExecutor(1, 1, 60, TimeUnit.SECONDS, new LinkedBlockingDeque<>()); + ome = createOME("Topic1"); + + encodedRecord = new byte[100]; + BlobContainerAsyncClient mockContainerAsyncClient = PowerMockito.mock(BlobContainerAsyncClient.class); + mockDataFileWriter = mock(DataFileWriter.class); + mockAzureBlobOutputStream = mock(AzureBlobOutputStream.class); + mockBlockBlobAsyncClient = PowerMockito.mock(BlockBlobAsyncClient.class); + when(mockBlockBlobAsyncClient.getBlobUrl()).thenReturn("https://samza.blob.core.windows.net/fake-blob-url"); + + azureBlobAvroWriter = + spy(new AzureBlobAvroWriter(mockContainerAsyncClient, mock(AzureBlobWriterMetrics.class), threadPool, THRESHOLD, + 60000, "test", mockDataFileWriter, mockAzureBlobOutputStream, mockBlockBlobAsyncClient, Long.MAX_VALUE, + Long.MAX_VALUE)); // keeping blob size and number of records unlimited + doReturn(encodedRecord).when(azureBlobAvroWriter).encodeRecord((IndexedRecord) ome.getMessage()); + } + @Test + public void testWrite() throws Exception { + int numberOfMessages = 10; + for (int i = 0; i < numberOfMessages; ++i) { + azureBlobAvroWriter.write(ome); + } + verify(mockDataFileWriter, times(numberOfMessages)).appendEncoded(ByteBuffer.wrap(encodedRecord)); + } + + @Test + public void testWriteGenericRecord() throws Exception { + OutgoingMessageEnvelope omeGenericRecord = createOMEGenericRecord("Topic1"); + doReturn(encodedRecord).when(azureBlobAvroWriter).encodeRecord((IndexedRecord) omeGenericRecord.getMessage()); + int numberOfMessages = 10; + for (int i = 0; i < numberOfMessages; ++i) { + azureBlobAvroWriter.write(omeGenericRecord); + } + verify(mockDataFileWriter, times(numberOfMessages)).appendEncoded(ByteBuffer.wrap(encodedRecord)); + } + + @Test + public void testWriteByteArray() throws Exception { + OutgoingMessageEnvelope omeEncoded = new OutgoingMessageEnvelope(new SystemStream(SYSTEM_NAME, "Topic1"), "randomString".getBytes()); + int numberOfMessages = 10; + azureBlobAvroWriter.write(ome); + for (int i = 0; i < numberOfMessages; ++i) { + azureBlobAvroWriter.write(omeEncoded); + } + verify(mockDataFileWriter).appendEncoded(ByteBuffer.wrap(encodedRecord)); + verify(mockDataFileWriter, times(numberOfMessages)).appendEncoded(ByteBuffer.wrap((byte[]) omeEncoded.getMessage())); + } + + @Test(expected = IllegalStateException.class) + public void testWriteByteArrayWithoutSchema() throws Exception { + azureBlobAvroWriter = + spy(new AzureBlobAvroWriter(PowerMockito.mock(BlobContainerAsyncClient.class), mock(AzureBlobWriterMetrics.class), + threadPool, THRESHOLD, 60000, "test", + null, null, null, 1000, 100)); + OutgoingMessageEnvelope omeEncoded = new OutgoingMessageEnvelope(new SystemStream(SYSTEM_NAME, "Topic1"), new byte[100]); + azureBlobAvroWriter.write(omeEncoded); + } + + @Test(expected = IOException.class) + public void testWriteWhenDataFileWriterFails() throws Exception { + doThrow(new IOException("Failed")).when(mockDataFileWriter).appendEncoded(ByteBuffer.wrap(encodedRecord)); + azureBlobAvroWriter.write(ome); + } + + @Test + public void testClose() throws Exception { + azureBlobAvroWriter.close(); + verify(mockDataFileWriter).close(); + verify(mockAzureBlobOutputStream).close(); + } + + @Test(expected = SamzaException.class) + public void testCloseWhenDataFileWriterFails() throws Exception { + doThrow(new IOException("Failed")).when(mockDataFileWriter).close(); + + azureBlobAvroWriter.flush(); + azureBlobAvroWriter.close(); + } + + @Test(expected = RuntimeException.class) + public void testCloseWhenOutputStreamFails() throws Exception { + doThrow(new RuntimeException("failed")).when(mockAzureBlobOutputStream).close(); + + azureBlobAvroWriter.close(); + } + + @Test + public void testFlush() throws Exception { + azureBlobAvroWriter.flush(); + verify(mockDataFileWriter).flush(); + } + + @Test(expected = IOException.class) + public void testFlushWhenDataFileWriterFails() throws Exception { + doThrow(new IOException("Failed")).when(mockDataFileWriter).flush(); + azureBlobAvroWriter.flush(); + } + + @Test + public void testMaxBlobSizeExceeded() throws Exception { + String blobUrlPrefix = "test"; + long maxBlobSize = 1000; + BlobContainerAsyncClient mockContainerClient = PowerMockito.mock(BlobContainerAsyncClient.class); + azureBlobAvroWriter = spy(new AzureBlobAvroWriter(mockContainerClient, + mock(AzureBlobWriterMetrics.class), threadPool, THRESHOLD, 60000, blobUrlPrefix, + null, null, null, maxBlobSize, 10)); + + when(mockAzureBlobOutputStream.getSize()).thenReturn((long) maxBlobSize - 1); + PowerMockito.whenNew(AzureBlobOutputStream.class).withAnyArguments().thenReturn(mockAzureBlobOutputStream); + PowerMockito.whenNew(DataFileWriter.class).withAnyArguments().thenReturn(mockDataFileWriter); + BlobAsyncClient mockBlobAsyncClient = mock(BlobAsyncClient.class); + doReturn(mockBlobAsyncClient).when(mockContainerClient).getBlobAsyncClient(anyString()); + doReturn(mockBlockBlobAsyncClient).when(mockBlobAsyncClient).getBlockBlobAsyncClient(); + // first OME creates the first blob + azureBlobAvroWriter.write(ome); + // Second OME creates the second blob because maxBlobSize is 1000 and mockAzureBlobOutputStream.getSize is 999. + azureBlobAvroWriter.write(ome); + + ArgumentCaptor<String> argument = ArgumentCaptor.forClass(String.class); + verify(mockContainerClient, times(2)).getBlobAsyncClient(argument.capture()); + argument.getAllValues().forEach(blobName -> { + Assert.assertTrue(blobName.contains(blobUrlPrefix)); + }); + List<String> allBlobNames = argument.getAllValues(); + Assert.assertNotEquals(allBlobNames.get(0), allBlobNames.get(1)); + } + + @Test + public void testRecordLimitExceeded() throws Exception { + String blobUrlPrefix = "test"; + long maxBlobSize = AzureBlobAvroWriter.DATAFILEWRITER_OVERHEAD + 1000; + long maxRecordsPerBlob = 10; + BlobContainerAsyncClient mockContainerClient = PowerMockito.mock(BlobContainerAsyncClient.class); + azureBlobAvroWriter = spy(new AzureBlobAvroWriter(mockContainerClient, + mock(AzureBlobWriterMetrics.class), threadPool, THRESHOLD, 60000, blobUrlPrefix, + null, null, null, maxBlobSize, maxRecordsPerBlob)); + + when(mockAzureBlobOutputStream.getSize()).thenReturn((long) 1); + PowerMockito.whenNew(AzureBlobOutputStream.class).withAnyArguments().thenReturn(mockAzureBlobOutputStream); + PowerMockito.whenNew(DataFileWriter.class).withAnyArguments().thenReturn(mockDataFileWriter); Review comment: Same as above about verifying where the data gets appended. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
