aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/me/xmrvizzy/skyblocker/utils/scheduler/SchedulerTest.java
blob: 3bc123378c88a0711154c2b23c8f4b76feac99cb (plain)
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
89
90
package me.xmrvizzy.skyblocker.utils.scheduler;

import org.apache.commons.lang3.mutable.MutableInt;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class SchedulerTest {
    @SuppressWarnings("deprecation")
    private final Scheduler scheduler = new Scheduler();
    private final MutableInt currentTick = new MutableInt(0);
    private final MutableInt cycleCount1 = new MutableInt(0);
    private final MutableInt cycleCount2 = new MutableInt(0);
    private final MutableInt cycleCount3 = new MutableInt(0);
    private final MutableInt cycleCount4 = new MutableInt(0);
    private final MutableInt cycleCount5 = new MutableInt(0);
    private final MutableInt cycleCount6 = new MutableInt(0);
    private final MutableInt cycleCount7 = new MutableInt(0);
    private final MutableInt cycleCount8 = new MutableInt(0);

    @Test
    public void testSchedule() {
        scheduler.schedule(() -> Assertions.assertEquals(0, currentTick.intValue()), 0);
        scheduler.schedule(() -> Assertions.assertEquals(1, currentTick.intValue()), 1);
        scheduler.schedule(() -> Assertions.assertEquals(2, currentTick.intValue()), 2);
        scheduler.schedule(() -> Assertions.assertEquals(10, currentTick.intValue()), 10);
        scheduler.schedule(() -> Assertions.assertEquals(20, currentTick.intValue()), 20);
        scheduler.schedule(() -> Assertions.assertEquals(50, currentTick.intValue()), 50);
        scheduler.schedule(() -> Assertions.assertEquals(100, currentTick.intValue()), 100);
        scheduler.schedule(() -> Assertions.assertEquals(123, currentTick.intValue()), 123);
        scheduler.scheduleCyclic(() -> {}, 1);
        scheduler.scheduleCyclic(() -> {}, 1);
        scheduler.scheduleCyclic(() -> {}, 1);
        scheduler.scheduleCyclic(() -> {}, 1);
        scheduler.scheduleCyclic(() -> {
            Assertions.assertEquals(cycleCount1.intValue(), currentTick.intValue());
            cycleCount1.increment();
        }, 1);
        scheduler.scheduleCyclic(() -> {
            Assertions.assertEquals(0, currentTick.intValue() % 10);
            Assertions.assertEquals(cycleCount2.intValue(), currentTick.intValue() / 10);
            cycleCount2.increment();
        }, 10);
        scheduler.scheduleCyclic(() -> {
            Assertions.assertEquals(0, currentTick.intValue() % 55);
            Assertions.assertEquals(cycleCount3.intValue(), currentTick.intValue() / 55);
            cycleCount3.increment();
        }, 55);
        scheduler.schedule(() -> scheduler.scheduleCyclic(() -> {
            Assertions.assertEquals(7, currentTick.intValue() % 10);
            Assertions.assertEquals(cycleCount4.intValue(), currentTick.intValue() / 10);
            cycleCount4.increment();
        }, 10), 7);
        scheduler.schedule(() -> scheduler.scheduleCyclic(() -> {
            Assertions.assertEquals(0, currentTick.intValue() % 75);
            Assertions.assertEquals(cycleCount5.intValue(), currentTick.intValue() / 75);
            cycleCount5.increment();
        }, 75), 0);
        scheduler.schedule(() -> scheduler.scheduleCyclic(() -> {
            Assertions.assertEquals(1, currentTick.intValue() % 99);
            Assertions.assertEquals(cycleCount6.intValue(), currentTick.intValue() / 99);
            cycleCount6.increment();
        }, 99), 1);
        scheduler.scheduleCyclic(() -> scheduler.schedule(() -> {
            Assertions.assertEquals(5, currentTick.intValue() % 10);
            Assertions.assertEquals(cycleCount7.intValue(), currentTick.intValue() / 10);
            cycleCount7.increment();
        }, 5), 10);
        scheduler.scheduleCyclic(() -> scheduler.schedule(() -> {
            Assertions.assertEquals(10, currentTick.intValue() % 55);
            Assertions.assertEquals(cycleCount8.intValue(), currentTick.intValue() / 55);
            cycleCount8.increment();
        }, 10), 55);
        while (currentTick.intValue() < 10_000_000) {
            tick();
        }
        Assertions.assertEquals(10000000, cycleCount1.intValue());
        Assertions.assertEquals(1000000, cycleCount2.intValue());
        Assertions.assertEquals(181819, cycleCount3.intValue());
        Assertions.assertEquals(1000000, cycleCount4.intValue());
        Assertions.assertEquals(133334, cycleCount5.intValue());
        Assertions.assertEquals(101011, cycleCount6.intValue());
        Assertions.assertEquals(1000000, cycleCount7.intValue());
        Assertions.assertEquals(181818, cycleCount8.intValue());
    }

    private void tick() {
        scheduler.tick();
        currentTick.increment();
    }
}