brooklyn-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From neykov <...@git.apache.org>
Subject [GitHub] brooklyn-server issue #449: Increase persistence timeout
Date Tue, 22 Nov 2016 17:24:50 GMT
Github user neykov commented on the issue:

    https://github.com/apache/brooklyn-server/pull/449
  
    For the record I'm adding the test case here. It's the plain java equivalent of the merged
test.
    ```
    
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.Writer;
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.concurrent.Callable;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.Future;
    
    public class FileWriteSimpleTest {
        private static final int THREAD_COUNT = 2;
        private static final long FILE_OPERATION_TIMEOUT_MS = 20000;
        private static final String TEST_FILE_CONTENT = generateContent();
        private static final ExecutorService executor = Executors.newFixedThreadPool(THREAD_COUNT);
    
        private static class FileWriterTask implements Callable<FileWriterTask> {
            @Override
            public FileWriterTask call() throws Exception {
                try {
                    File tmp = File.createTempFile(getClass().getName() + ".", ".txt");
                    try(Writer out = new FileWriter(tmp)) {
                        out.write(TEST_FILE_CONTENT);
                    }
                    File dst = new File(tmp.getParentFile(), tmp.getName() + ".renamed");
                    if (!tmp.renameTo(dst)) {
                        throw new IOException("Couldn't rename file " + tmp + " to " + dst);
                    }
                    if (!dst.delete()) {
                        throw new IOException("Couldn't delete file " + tmp);
                    }
                    return this;
                } catch (IOException e) {
                    throw new IllegalStateException(e);
                }
            }
        };
        
        public void testSimpleOperationsDelay() throws Exception {
            Collection<Future<FileWriterTask>> tasks = new ArrayList<>(THREAD_COUNT);
            for (int i = 0; i < THREAD_COUNT; i++) {
                tasks.add(executor.submit(new FileWriterTask()));
            }
            long timeStart = System.currentTimeMillis();
            for (Future<FileWriterTask> task : tasks) {
                task.get();
            }
            long timeEnd = System.currentTimeMillis();
            long timeSpent = timeEnd - timeStart;
            if (timeSpent > FILE_OPERATION_TIMEOUT_MS) {
                throw new IllegalStateException("Timed out waiting for tasks - " + timeSpent);
            }
        }
        
        public void testSimpleOperationsDelayManyTimes() throws Exception {
            try {
                for (int i = 1; i <= 5000; i++) {
                    if (i % 100 == 0) {
                        System.out.println("Iteration " + i);
                    }
                    testSimpleOperationsDelay();
                }
            } finally {
                executor.shutdown();
            }
        }
    
        private static String generateContent() {
            final char[] charArray = new char[4096];
            java.util.Arrays.fill(charArray, ' ');
            return new String(charArray);
        }
    
        public static void main(String[] args) throws Exception {
            new FileWriteSimpleTest().testSimpleOperationsDelayManyTimes();
        }
    
    }
    ```


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message