|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.apache.beam.io.debezium; |
| 19 | + |
| 20 | +import static org.junit.Assert.assertEquals; |
| 21 | +import static org.junit.Assert.assertFalse; |
| 22 | +import static org.junit.Assert.assertNotNull; |
| 23 | +import static org.junit.Assert.assertNull; |
| 24 | +import static org.junit.Assert.assertThrows; |
| 25 | +import static org.junit.Assert.assertTrue; |
| 26 | + |
| 27 | +import java.io.ByteArrayInputStream; |
| 28 | +import java.io.ByteArrayOutputStream; |
| 29 | +import java.io.File; |
| 30 | +import java.io.ObjectInputStream; |
| 31 | +import java.io.ObjectOutputStream; |
| 32 | +import java.nio.charset.StandardCharsets; |
| 33 | +import java.nio.file.Files; |
| 34 | +import java.nio.file.Paths; |
| 35 | +import java.util.Map; |
| 36 | +import org.apache.beam.sdk.io.FileSystems; |
| 37 | +import org.apache.beam.sdk.options.PipelineOptionsFactory; |
| 38 | +import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.ImmutableMap; |
| 39 | +import org.junit.Before; |
| 40 | +import org.junit.Rule; |
| 41 | +import org.junit.Test; |
| 42 | +import org.junit.rules.TemporaryFolder; |
| 43 | +import org.junit.runner.RunWith; |
| 44 | +import org.junit.runners.JUnit4; |
| 45 | + |
| 46 | +/** Unit tests for {@link FileSystemOffsetRetainer}. */ |
| 47 | +@RunWith(JUnit4.class) |
| 48 | +public class FileSystemOffsetRetainerTest { |
| 49 | + |
| 50 | + @Rule public TemporaryFolder tmpFolder = new TemporaryFolder(); |
| 51 | + |
| 52 | + @Before |
| 53 | + public void setUp() { |
| 54 | + FileSystems.setDefaultPipelineOptions(PipelineOptionsFactory.create()); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void testLoadOffsetReturnNullWhenFileIsMissing() { |
| 59 | + String path = tmpFolder.getRoot().getAbsolutePath() + "/nonexistent.json"; |
| 60 | + assertNull(FileSystemOffsetRetainer.of(path).loadOffset()); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void testLoadOffsetThrowsWhenFileIsUnreadable() throws Exception { |
| 65 | + String path = tmpFolder.getRoot().getAbsolutePath() + "/offset.json"; |
| 66 | + FileSystemOffsetRetainer retainer = FileSystemOffsetRetainer.of(path); |
| 67 | + retainer.saveOffset(ImmutableMap.of("lsn", "100")); |
| 68 | + |
| 69 | + // Corrupt the file so JSON parsing fails. |
| 70 | + Files.newBufferedWriter(Paths.get(path), StandardCharsets.UTF_8).close(); // truncate to empty |
| 71 | + assertThrows(RuntimeException.class, retainer::loadOffset); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void testSaveAndLoadOffsetRoundTrip() { |
| 76 | + String path = tmpFolder.getRoot().getAbsolutePath() + "/offset.json"; |
| 77 | + FileSystemOffsetRetainer retainer = FileSystemOffsetRetainer.of(path); |
| 78 | + |
| 79 | + retainer.saveOffset(ImmutableMap.of("file", "binlog.000001", "pos", "156")); |
| 80 | + |
| 81 | + Map<String, Object> loaded = retainer.loadOffset(); |
| 82 | + assertNotNull(loaded); |
| 83 | + assertEquals("binlog.000001", loaded.get("file")); |
| 84 | + assertEquals("156", loaded.get("pos")); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void testSaveOffsetSkipsWriteWhenOffsetUnchanged() throws Exception { |
| 89 | + String path = tmpFolder.getRoot().getAbsolutePath() + "/offset.json"; |
| 90 | + FileSystemOffsetRetainer retainer = FileSystemOffsetRetainer.of(path); |
| 91 | + Map<String, Object> offset = ImmutableMap.of("lsn", "100"); |
| 92 | + |
| 93 | + retainer.saveOffset(offset); |
| 94 | + long modifiedAfterFirstSave = new File(path).lastModified(); |
| 95 | + |
| 96 | + // Second call with the same offset should not touch the file. |
| 97 | + Thread.sleep(10); // ensure mtime would differ if a write occurred |
| 98 | + retainer.saveOffset(offset); |
| 99 | + assertEquals(modifiedAfterFirstSave, new File(path).lastModified()); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void testSaveOffsetLeavesNoTmpFile() { |
| 104 | + String path = tmpFolder.getRoot().getAbsolutePath() + "/offset.json"; |
| 105 | + FileSystemOffsetRetainer.of(path).saveOffset(ImmutableMap.of("lsn", "28160840")); |
| 106 | + |
| 107 | + assertTrue("Final offset file should exist", new File(path).exists()); |
| 108 | + assertFalse("Temp file should not remain after rename", new File(path + ".tmp").exists()); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void testSerializedRetainerCanLoadAfterDeserialization() throws Exception { |
| 113 | + String path = tmpFolder.getRoot().getAbsolutePath() + "/offset.json"; |
| 114 | + FileSystemOffsetRetainer original = FileSystemOffsetRetainer.of(path); |
| 115 | + original.saveOffset(ImmutableMap.of("lsn", "12345")); |
| 116 | + |
| 117 | + ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| 118 | + try (ObjectOutputStream oos = new ObjectOutputStream(bos)) { |
| 119 | + oos.writeObject(original); |
| 120 | + } |
| 121 | + FileSystemOffsetRetainer deserialized; |
| 122 | + try (ObjectInputStream ois = |
| 123 | + new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()))) { |
| 124 | + deserialized = (FileSystemOffsetRetainer) ois.readObject(); |
| 125 | + } |
| 126 | + |
| 127 | + Map<String, Object> loaded = deserialized.loadOffset(); |
| 128 | + assertNotNull(loaded); |
| 129 | + assertEquals("12345", loaded.get("lsn")); |
| 130 | + } |
| 131 | +} |
0 commit comments