|
| 1 | +package com.fasterxml.jackson.dataformat.avro; |
| 2 | + |
| 3 | +import java.io.*; |
| 4 | + |
| 5 | +import com.fasterxml.jackson.databind.MappingIterator; |
| 6 | +import com.fasterxml.jackson.databind.SequenceWriter; |
| 7 | + |
| 8 | +/** |
| 9 | + * Simple tests for some testable aspects of concurrent usage. |
| 10 | + * Specifically tries to ensure that usage of multiple parsers, |
| 11 | + * generators, from same thread, does not cause problems with |
| 12 | + * reuse of certain components. |
| 13 | + */ |
| 14 | +public class ConcurrencyTest extends AvroTestBase |
| 15 | +{ |
| 16 | + private final AvroMapper MAPPER = getMapper(); |
| 17 | + |
| 18 | + private final AvroSchema EMPL_SCHEMA; |
| 19 | + |
| 20 | + public ConcurrencyTest() throws IOException { |
| 21 | + EMPL_SCHEMA = getEmployeeSchema(); |
| 22 | + } |
| 23 | + |
| 24 | + // Simple test that creates 2 encoders and uses them in interleaved manner. |
| 25 | + // This should tease out simplest problems with possible encoder reuse. |
| 26 | + public void testMultipleEncoders() throws Exception |
| 27 | + { |
| 28 | + ByteArrayOutputStream b1 = new ByteArrayOutputStream(); |
| 29 | + ByteArrayOutputStream b2 = new ByteArrayOutputStream(); |
| 30 | + SequenceWriter sw1 = MAPPER.writer(EMPL_SCHEMA) |
| 31 | + .writeValues(b1); |
| 32 | + SequenceWriter sw2 = MAPPER.writer(EMPL_SCHEMA) |
| 33 | + .writeValues(b2); |
| 34 | + |
| 35 | + for (int i = 0; i < 200; ++i) { |
| 36 | + _writeEmpl(sw1, "foo", i); |
| 37 | + _writeEmpl(sw2, "foo", i); |
| 38 | + } |
| 39 | + sw1.close(); |
| 40 | + sw2.close(); |
| 41 | + assertEquals(b1.size(), b2.size()); |
| 42 | + // value just verified once, but since Avro format stable should remain stable |
| 43 | + assertEquals(6926, b1.size()); |
| 44 | + } |
| 45 | + |
| 46 | + public void testMultipleDecodersBlock() throws Exception { |
| 47 | + _testMultipleDecoders(false); |
| 48 | + } |
| 49 | + |
| 50 | + public void testMultipleDecodersStreaming() throws Exception { |
| 51 | + _testMultipleDecoders(true); |
| 52 | + } |
| 53 | + |
| 54 | + private void _testMultipleDecoders(boolean useStream) throws Exception |
| 55 | + { |
| 56 | + final int ROUNDS = 40; |
| 57 | + // Here let's do encoding linearly, to remove coupling with other test(s) |
| 58 | + ByteArrayOutputStream b = new ByteArrayOutputStream(); |
| 59 | + SequenceWriter sw = MAPPER.writer(EMPL_SCHEMA) |
| 60 | + .writeValues(b); |
| 61 | + for (int i = 0; i < ROUNDS; ++i) { |
| 62 | + _writeEmpl(sw, "a", i); |
| 63 | + } |
| 64 | + sw.close(); |
| 65 | + final byte[] b1 = b.toByteArray(); |
| 66 | + |
| 67 | + b = new ByteArrayOutputStream(); |
| 68 | + sw = MAPPER.writer(EMPL_SCHEMA) |
| 69 | + .writeValues(b); |
| 70 | + for (int i = 0; i < ROUNDS; ++i) { |
| 71 | + _writeEmpl(sw, "b", i); |
| 72 | + } |
| 73 | + sw.close(); |
| 74 | + final byte[] b2 = b.toByteArray(); |
| 75 | + |
| 76 | + MappingIterator<Employee> it1, it2; |
| 77 | + |
| 78 | + if (useStream) { |
| 79 | + it1 = MAPPER.readerFor(Employee.class).with(EMPL_SCHEMA) |
| 80 | + .readValues(b1); |
| 81 | + it2 = MAPPER.readerFor(Employee.class).with(EMPL_SCHEMA) |
| 82 | + .readValues(b2); |
| 83 | + } else { |
| 84 | + it1 = MAPPER.readerFor(Employee.class).with(EMPL_SCHEMA) |
| 85 | + .readValues(new ByteArrayInputStream(b1)); |
| 86 | + it2 = MAPPER.readerFor(Employee.class).with(EMPL_SCHEMA) |
| 87 | + .readValues(new ByteArrayInputStream(b2)); |
| 88 | + } |
| 89 | + |
| 90 | + for (int i = 0; i < 40; ++i) { |
| 91 | + assertTrue(it1.hasNextValue()); |
| 92 | + assertTrue(it2.hasNextValue()); |
| 93 | + Employee e1 = it1.nextValue(); |
| 94 | + Employee e2 = it2.nextValue(); |
| 95 | + |
| 96 | + assertEquals("Empl"+i+"a", e1.name); |
| 97 | + assertEquals("Empl"+i+"b", e2.name); |
| 98 | + assertEquals(10+i, e1.age); |
| 99 | + assertEquals(10+i, e2.age); |
| 100 | + } |
| 101 | + assertFalse(it1.hasNextValue()); |
| 102 | + assertFalse(it2.hasNextValue()); |
| 103 | + it1.close(); |
| 104 | + it2.close(); |
| 105 | + } |
| 106 | + |
| 107 | + private void _writeEmpl(SequenceWriter sw, String type, int index) throws IOException { |
| 108 | + sw.write(_empl(type, index)); |
| 109 | + } |
| 110 | + |
| 111 | + private Employee _empl(String type, int index) { |
| 112 | + return new Employee("Empl"+index+type, 10+index, new String[] { "empl"+index+"@company.com" }, null); |
| 113 | + } |
| 114 | +} |
0 commit comments