mirror of https://github.com/micromata/borgbackup-butler.git

Kai Reinhard
28.16.2018 62b938d31692414f86c8f306b855e2f3bc7cf536
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package de.micromata.borgbutler.jobs;
 
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.List;
 
import static org.junit.jupiter.api.Assertions.assertEquals;
 
public class JobQueueTest {
    private Logger log = LoggerFactory.getLogger(JobQueueTest.class);
    private static String bashScript = "#!/bin/bash\n" +
            "COUNTER=0\n" +
            "while [ $COUNTER -lt $1 ]; do\n" +
            "  if [ $COUNTER -eq $2 ]; then\n" +
            "    echo Error on counter $COUNTER >&2\n" +
            "    exit 2\n" +
            "  fi\n" +
            "  sleep 0.1\n" +
            "  echo The counter is $COUNTER >&2\n" +
            "  let COUNTER=COUNTER+1 \n" +
            "done\n" +
            "echo $COUNTER\n";
 
    private static File file;
 
    @BeforeAll
    static void createScript() throws IOException {
        file = File.createTempFile("counter", ".sh");
        file.deleteOnExit();
        FileUtils.write(file, bashScript, Charset.forName("UTF-8"));
        Files.setPosixFilePermissions(file.toPath(), PosixFilePermissions.fromString("rwxr-xr-x"));
    }
 
    @Test
    void queueTest() {
        JobQueue queue = new JobQueue();
        assertEquals(0, queue.getQueueSize());
        queue.append(new TestJob(10, file));
        assertEquals(1, queue.getQueueSize());
        queue.append(new TestJob(5, 2, file));
        assertEquals(2, queue.getQueueSize());
        queue.append(new TestJob(10, file));
        assertEquals(2, queue.getQueueSize());
        TestJob job = (TestJob) queue.getQueuedJob(10);
        int counter = 100;
        while (job.getStatus() != AbstractJob.Status.RUNNING && counter-- > 0) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException ex) {
                log.error(ex.getMessage(), ex);
            }
        }
        assertEquals(AbstractJob.Status.RUNNING, job.getStatus());
        job = (TestJob) queue.getQueuedJob(5);
        assertEquals(AbstractJob.Status.QUEUED, job.getStatus());
        counter = 100;
        while (job.getStatus() != AbstractJob.Status.RUNNING && counter-- > 0) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException ex) {
                log.error(ex.getMessage(), ex);
            }
        }
        queue.append(new TestJob(10, file));
        job = (TestJob) queue.getQueuedJob(10);
        assertEquals(AbstractJob.Status.QUEUED, job.getStatus());
        queue.waitForQueue(10);
        assertEquals(0, queue.getQueueSize());
        List<AbstractJob> doneJobs = queue.getDoneJobs();
        assertEquals(3, doneJobs.size());
        check(((TestJob)doneJobs.get(0)), AbstractJob.Status.DONE, "10");
        check(((TestJob)doneJobs.get(1)), AbstractJob.Status.FAILED, "10");
        check(((TestJob)doneJobs.get(2)), AbstractJob.Status.DONE, "10");
    }
 
    private void check(TestJob job, AbstractJob.Status status, String result) {
        assertEquals(status, job.getStatus());
    }
}