View Javadoc
1   package emissary.pickup;
2   
3   import emissary.command.FeedCommand;
4   import emissary.core.Namespace;
5   import emissary.directory.EmissaryNode;
6   import emissary.directory.IDirectoryPlace;
7   import emissary.test.core.junit5.FunctionalTest;
8   
9   import jakarta.annotation.Nullable;
10  import org.apache.commons.io.FileUtils;
11  import org.junit.jupiter.api.AfterEach;
12  import org.junit.jupiter.api.BeforeEach;
13  import org.junit.jupiter.api.Test;
14  
15  import java.io.File;
16  import java.io.FileOutputStream;
17  import java.util.ArrayList;
18  import java.util.List;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertTrue;
22  
23  class FTestWorkSpaceMaxBundleSize extends FunctionalTest {
24      @Nullable
25      private MyWorkSpace space = null;
26      @Nullable
27      private IDirectoryPlace peer = null;
28  
29      // Workspace input and output directories
30      private File inarea1;
31      private File inareadir1;
32      private File outarea1;
33      private File holdarea;
34  
35      private final List<File> workingFiles = new ArrayList<>();
36      private final List<String> workingFilePaths = new ArrayList<>();
37  
38      @Override
39      @BeforeEach
40      public void setUp() throws Exception {
41          logger.debug("Starting WorkSpace tests");
42  
43          // Set up a directory struction with two files to be processed
44          // rom each workspace
45          inarea1 = new File(TMPDIR + "/test/space/in");
46          inarea1.mkdirs();
47  
48          outarea1 = new File(TMPDIR + "/test/space/out");
49          outarea1.mkdirs();
50  
51          holdarea = new File(TMPDIR, "/data/HoldData");
52          holdarea.mkdirs();
53  
54          File testfile = File.createTempFile("temp1", ".dat", inarea1);
55          workingFiles.add(testfile);
56          workingFilePaths.add(testfile.getName());
57          testfile.deleteOnExit();
58  
59          inareadir1 = new File(inarea1, "subdir1");
60          inareadir1.mkdirs();
61          inareadir1.deleteOnExit();
62          File testfile2 = File.createTempFile("temp2", ".dat", inareadir1);
63          workingFiles.add(testfile2);
64          workingFilePaths.add("subdir1/" + testfile2.getName());
65          testfile2.deleteOnExit();
66          File testfile3 = File.createTempFile("temp3", ".dat", inarea1);
67          workingFiles.add(testfile3);
68          workingFilePaths.add(testfile3.getName());
69          testfile3.deleteOnExit();
70          FileOutputStream os = new FileOutputStream(testfile);
71          os.write("This is a test".getBytes());
72          os.close();
73          os = new FileOutputStream(testfile2);
74          os.write("This is an even bigger test file!".getBytes());
75          os.close();
76          os = new FileOutputStream(testfile3);
77          os.write("This is a 3rd test.".getBytes());
78          os.close();
79  
80          // start jetty and directory services
81          // TODO These FTestWorkSpace* tests will compile now but need to be totally reworked due to the way we
82          // start/stop emissary
83          startJetty(8005);
84          // Start a second client to keep things happy
85          peer = startDirectory(9005);
86          peer.heartbeatRemoteDirectory(directory.getKey());
87          directory.heartbeatRemoteDirectory(peer.getKey());
88  
89          System.setProperty(EmissaryNode.NODE_PORT_PROPERTY, "" + 8005);
90          logger.debug("WorkSpace test setup completed");
91      }
92  
93      @Override
94      @AfterEach
95      public void tearDown() throws Exception {
96          if (space != null) {
97              space.stop();
98              space = null;
99          }
100 
101         demolishServer();
102 
103         // Clean up directories
104         FileUtils.deleteDirectory(inarea1.getParentFile());
105         FileUtils.deleteDirectory(outarea1);
106 
107         super.tearDown();
108     }
109 
110     private void createWorkspace(String namespace, int maxCount, long maxBytes) throws Exception {
111         // Create and configure a WorkSpace
112         space =
113                 new MyWorkSpace(FeedCommand.parse(FeedCommand.class, new String[] {"-nsname", namespace, "-c", TMPDIR, "-i",
114                         TMPDIR + "/test/space/in:10"}));
115         space.setEatPrefix(TMPDIR + "/test/space/in");
116         space.setOutputRoot(TMPDIR + "/test/space/out");
117         space.setCaseId("space1case");
118         space.setLoop(false);
119         space.setPauseTime(10);// millis
120         space.setRetryStrategy(true);
121         space.setDirectoryProcessing(false);
122         space.setFpm(maxCount);
123         space.setBpm(maxBytes);
124 
125         assertTrue(Namespace.exists("http://localhost:8005/" + namespace), "WorkSpace should exist in namespace");
126 
127         assertEquals(0, space.getFilesProcessed(), "No files proessed");
128         assertEquals(0, space.getBytesProcessed(), "No bytes proessed");
129         assertEquals(0, space.getBundlesProcessed(), "No bundles proessed");
130         assertEquals(0, space.getOutboundQueueSize(), "Outbound queue count in " + space.getKey());
131 
132     }
133 
134     @SuppressWarnings("ThreadPriorityCheck")
135     private void detachWorkspace(String threadName) {
136         // Create a thread and run the workspace detached
137         Thread tspacethr = new Thread(space, threadName);
138         tspacethr.setDaemon(false);
139         tspacethr.start();
140         logger.debug("WorkSpace is started and detached on thread " + threadName);
141         Thread.yield();
142     }
143 
144 
145     @Test
146     void testMaxFiles() throws Exception {
147         createWorkspace("testMaxFiles", 2, -1);
148         detachWorkspace("testMaxFiles");
149 
150         pause(500);
151 
152         assertEquals(3, space.getFilesProcessed(), "files processed on " + space.getKey());
153         assertEquals(66, space.getBytesProcessed(), "bytes processed on " + space.getKey());
154         assertEquals(2, space.getBundlesProcessed(), "bundles processed on " + space.getKey());
155         assertEquals(2, space.getOutboundQueueSize(), "Outbound queue count in " + space.getKey());
156     }
157 
158     @Test
159     void testMaxBytes() throws Exception {
160         createWorkspace("testMaxBytes", -1, 10);
161         detachWorkspace("testMaxBytes");
162 
163         pause(500);
164 
165         assertEquals(3, space.getFilesProcessed(), "files processed on " + space.getKey());
166         assertEquals(66, space.getBytesProcessed(), "bytes processed on " + space.getKey());
167         assertEquals(3, space.getBundlesProcessed(), "bundles processed on " + space.getKey());
168         assertEquals(3, space.getOutboundQueueSize(), "Outbound queue count in " + space.getKey());
169     }
170 
171     @Test
172     void testMaxBoth() throws Exception {
173         createWorkspace("testMaxBoth", 3, 20);
174         detachWorkspace("testMaxBoth");
175 
176         pause(1000);
177 
178         assertEquals(3, space.getFilesProcessed(), "files processed on " + space.getKey());
179         assertEquals(66, space.getBytesProcessed(), "bytes processed on " + space.getKey());
180         assertEquals(2, space.getBundlesProcessed(), "bundles processed on " + space.getKey());
181         assertEquals(2, space.getOutboundQueueSize(), "Outbound queue count in " + space.getKey());
182     }
183 
184 
185     @SuppressWarnings("unused")
186     // test class
187     private static final class MyWorkSpace extends WorkSpace {
188         public MyWorkSpace() throws Exception {
189             super();
190         }
191 
192         public MyWorkSpace(FeedCommand command) {
193             super(command);
194         }
195 
196         public void setFpm(int value) {
197             this.filesPerMessage = value;
198         }
199 
200         public void setBpm(long value) {
201             this.maxBundleSize = value;
202         }
203 
204         public int getFpm() {
205             return this.filesPerMessage;
206         }
207 
208         public long getBpm() {
209             return this.maxBundleSize;
210         }
211     }
212 }