|
| 1 | +/** |
| 2 | + * Copyright DataStax, Inc 2021. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.datastax.oss.cdc.agent; |
| 17 | + |
| 18 | +import com.datastax.oss.dsbulk.tests.utils.FileUtils; |
| 19 | +import org.junit.jupiter.api.AfterEach; |
| 20 | +import org.junit.jupiter.api.BeforeEach; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | + |
| 23 | +import java.io.File; |
| 24 | +import java.io.IOException; |
| 25 | +import java.nio.file.Files; |
| 26 | +import java.nio.file.Path; |
| 27 | +import java.nio.file.WatchEvent; |
| 28 | +import java.time.Duration; |
| 29 | +import java.util.Set; |
| 30 | +import java.util.concurrent.BlockingQueue; |
| 31 | +import java.util.concurrent.LinkedBlockingQueue; |
| 32 | +import java.util.stream.Collectors; |
| 33 | +import java.util.stream.Stream; |
| 34 | + |
| 35 | +import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE; |
| 36 | +import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY; |
| 37 | +import static java.nio.file.StandardWatchEventKinds.OVERFLOW; |
| 38 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 39 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 40 | + |
| 41 | +public class AbstractDirectoryWatcherTest { |
| 42 | + |
| 43 | + private Path cdcDir; |
| 44 | + |
| 45 | + @BeforeEach |
| 46 | + public void init() throws IOException { |
| 47 | + cdcDir = Files.createTempDirectory("cdc"); |
| 48 | + } |
| 49 | + |
| 50 | + @AfterEach |
| 51 | + void deleteTempDirs() { |
| 52 | + if (cdcDir != null && Files.exists(cdcDir)) { |
| 53 | + FileUtils.deleteDirectory(cdcDir); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void testPoll() throws InterruptedException, IOException { |
| 59 | + // given |
| 60 | + Set<WatchEvent.Kind<?>> watchedEvents = Stream.of(ENTRY_CREATE, ENTRY_MODIFY, OVERFLOW).collect(Collectors.toSet()); |
| 61 | + final BlockingQueue<File> commitLogQueue = new LinkedBlockingQueue<>(); |
| 62 | + |
| 63 | + AbstractDirectoryWatcher watcher = new AbstractDirectoryWatcher(cdcDir, |
| 64 | + Duration.ofSeconds(20), // avoid flakiness, usually completes in ~10 seconds |
| 65 | + watchedEvents) { |
| 66 | + @Override |
| 67 | + void handleEvent(WatchEvent<?> event, Path path) { |
| 68 | + commitLogQueue.add(path.toFile()); |
| 69 | + } |
| 70 | + }; |
| 71 | + File commitLog = new File(cdcDir.toFile(), "CommitLog-6-1.log"); |
| 72 | + commitLog.createNewFile(); |
| 73 | + |
| 74 | + // when |
| 75 | + watcher.poll(); // can block up to 20 seconds |
| 76 | + |
| 77 | + // then |
| 78 | + assertEventHandled(commitLogQueue, commitLog); |
| 79 | + |
| 80 | + // write another file |
| 81 | + File commitLog2 = new File(cdcDir.toFile(), "CommitLog-6-2.log"); |
| 82 | + commitLog2.createNewFile(); |
| 83 | + |
| 84 | + // when |
| 85 | + watcher.poll(); // can block up to 20 seconds |
| 86 | + |
| 87 | + // then |
| 88 | + assertEventHandled(commitLogQueue, commitLog2); |
| 89 | + } |
| 90 | + |
| 91 | + private void assertEventHandled(BlockingQueue<File> commitLogQueue, File expectedFile) { |
| 92 | + File actualFile = commitLogQueue.poll(); |
| 93 | + assertNotNull(actualFile); |
| 94 | + assertEquals(commitLogQueue.size(), 0); |
| 95 | + assertEquals(expectedFile, actualFile); |
| 96 | + } |
| 97 | +} |
0 commit comments