Search in sources :

Example 1 with BasicTestPortReader

use of com.ociweb.iot.hardware.impl.test.BasicTestPortReader in project FogLight-Examples by oci-pronghorn.

the class AppTest method testApp.

@Test
public void testApp() {
    FogRuntime runtime = FogRuntime.test(new IoTApp());
    ScriptedNonThreadScheduler scheduler = (ScriptedNonThreadScheduler) runtime.getScheduler();
    scheduler.startup();
    TestHardware hardware = (TestHardware) runtime.getHardware();
    hardware.portReader = new BasicTestPortReader();
    int iterations = 4;
    boolean isFirst = true;
    int expected = 0;
    long lastTime = 0;
    while (iterations > 0) {
        scheduler.run();
        long time = hardware.getLastTime(IoTApp.LED_PORT);
        if (0 != time) {
            iterations--;
            assertEquals(expected, 1 & hardware.read(IoTApp.LED_PORT));
            expected = 1 & (expected + 1);
            if (0 != lastTime) {
                long durationMs = (time - lastTime);
                if (!isFirst) {
                    assertTrue(Long.toString(durationMs), // first difference may be short because first transition is later due to startup.
                    durationMs >= 400);
                } else {
                    isFirst = false;
                }
                assertTrue(durationMs <= 750);
            }
            lastTime = time;
            hardware.clearCaputuredLastTimes();
            hardware.clearCaputuredHighs();
        }
    }
}
Also used : ScriptedNonThreadScheduler(com.ociweb.pronghorn.stage.scheduling.ScriptedNonThreadScheduler) TestHardware(com.ociweb.iot.hardware.impl.test.TestHardware) BasicTestPortReader(com.ociweb.iot.hardware.impl.test.BasicTestPortReader) FogRuntime(com.ociweb.iot.maker.FogRuntime) Test(org.junit.Test)

Example 2 with BasicTestPortReader

use of com.ociweb.iot.hardware.impl.test.BasicTestPortReader in project FogLight-Examples by oci-pronghorn.

the class AppTest method testApp.

@Test
public void testApp() {
    FogRuntime runtime = FogRuntime.test(new IoTApp());
    ScriptedNonThreadScheduler scheduler = (ScriptedNonThreadScheduler) runtime.getScheduler();
    TestHardware hardware = (TestHardware) runtime.getHardware();
    hardware.portReader = new BasicTestPortReader();
    hardware.clearI2CWriteCount();
    // 970 will give us 200 BPM and a delay of 300 ms
    hardware.write(IoTApp.ROTARY_ANGLE_PORT, 970);
    scheduler.startup();
    long lastTime = 0;
    int ticks = 5;
    hardware.clearCaputuredFirstTimes();
    hardware.clearCaputuredHighs();
    boolean isMetronomeRunning = false;
    long startTime = System.currentTimeMillis();
    while (ticks > 0) {
        scheduler.run();
        long time = hardware.getFirstTime(IoTApp.BUZZER_PORT);
        if (0 != time) {
            int high = hardware.getCapturedHigh(IoTApp.BUZZER_PORT);
            if (0 != high) {
                ticks--;
                if (0 != lastTime) {
                    if (isMetronomeRunning) {
                        long durationMs = (time - lastTime);
                        // due to assertions and garbage when unit tests are run we can not be so strict here
                        int overheadForTesting = 10;
                        int window = 300;
                        // this is a little too quick now... and under the threshold.
                        // assertTrue(durationMs+" at "+time, durationMs>=(window-overheadForTesting));
                        assertTrue(durationMs + " at " + time, durationMs <= (window + overheadForTesting));
                    } else {
                        isMetronomeRunning = true;
                    }
                }
                lastTime = time;
                hardware.clearCaputuredFirstTimes();
                hardware.clearCaputuredHighs();
            } else {
            // low
            }
        }
    }
    int count = hardware.getI2CWriteCount();
    System.out.println(count);
    int c = count;
    while (c > 0) {
        hardware.outputLastI2CWrite(System.out, c--).append("\n");
    }
    assertEquals("Did not find all the ticks.", 0, ticks);
    scheduler.shutdown();
    scheduler.awaitTermination(10, TimeUnit.SECONDS);
}
Also used : ScriptedNonThreadScheduler(com.ociweb.pronghorn.stage.scheduling.ScriptedNonThreadScheduler) TestHardware(com.ociweb.iot.hardware.impl.test.TestHardware) BasicTestPortReader(com.ociweb.iot.hardware.impl.test.BasicTestPortReader) FogRuntime(com.ociweb.iot.maker.FogRuntime) Test(org.junit.Test)

Aggregations

BasicTestPortReader (com.ociweb.iot.hardware.impl.test.BasicTestPortReader)2 TestHardware (com.ociweb.iot.hardware.impl.test.TestHardware)2 FogRuntime (com.ociweb.iot.maker.FogRuntime)2 ScriptedNonThreadScheduler (com.ociweb.pronghorn.stage.scheduling.ScriptedNonThreadScheduler)2 Test (org.junit.Test)2