DriverStation.getAlliance() in gradle test

We’re gradually started to make more use of ‘gradle test’, and hit a case that we didn’t design for.

We have some code to test that needs to return different results if we are red or blue. The code tests DriverStation.getAlliance() to determine which to do. We are not calling getAlliance() before it is valid (we read this thread).

Is there a way in a gradle test to force DriverStation.getAlliance() to red or blue?

DriverStationSim.setAllianceStationId()

2 Likes

Thanks. Brings up another issue.

I’ve been having some trouble in tests with wpiHal blowing up, and this test case is one thing that triggers it:

import org.junit.jupiter.api.Test;

import edu.wpi.first.hal.AllianceStationID;
import edu.wpi.first.wpilibj.DriverStation;
import edu.wpi.first.wpilibj.simulation.DriverStationSim;

public class DriverStationAllianceTest {

    @Test
    public void test00() {
        DriverStationSim.setAllianceStationId(AllianceStationID.Blue1);
        System.out.println (DriverStation.getAlliance() + " " + DriverStation.getLocation());
        DriverStationSim.setAllianceStationId(AllianceStationID.Red3);
        System.out.println (DriverStation.getAlliance() + " " + DriverStation.getLocation());
    }
}
> Task :test
#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00007ffdb05141c8, pid=9264, tid=8896
#
# JRE version: OpenJDK Runtime Environment Temurin-17.0.5+8 (17.0.5+8) (build 17.0.5+8)
# Java VM: OpenJDK 64-Bit Server VM Temurin-17.0.5+8 (17.0.5+8, mixed mode, sharing, tiered, compressed oops, compressed class ptrs, g1 gc, windows-amd64)
# Problematic frame:
# C  [wpiHal.dll+0x41c8]
#
# No core dump will be written. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as:
# C:\Users\dwegs\Documents\FRC3620.2023\FRC3620_2023_SteveThePirate\hs_err_pid9264.log
#
# If you would like to submit a bug report, please visit:
#   https://github.com/adoptium/adoptium-support/issues
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#

Unexpected exception thrown.
org.gradle.internal.remote.internal.MessageIOException: Could not write '/127.0.0.1:46352'.
        at org.gradle.internal.remote.internal.inet.SocketConnection.flush(SocketConnection.java:140)
        at org.gradle.internal.remote.internal.hub.MessageHub$ConnectionDispatch.run(MessageHub.java:333)
        at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:64)       
        at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:48)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
        at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.io.IOException: Connection reset by peer
        at java.base/sun.nio.ch.SocketDispatcher.write0(Native Method)
        at java.base/sun.nio.ch.SocketDispatcher.write(SocketDispatcher.java:54)
        at java.base/sun.nio.ch.IOUtil.writeFromNativeBuffer(IOUtil.java:132)
        at java.base/sun.nio.ch.IOUtil.write(IOUtil.java:76)
        at java.base/sun.nio.ch.IOUtil.write(IOUtil.java:53)
        at java.base/sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:532)
        at org.gradle.internal.remote.internal.inet.SocketConnection$SocketOutputStream.writeWithNonBlockingRetry(SocketConnection.java:279)
        at org.gradle.internal.remote.internal.inet.SocketConnection$SocketOutputStream.writeBufferToChannel(SocketConnection.java:267)
        at org.gradle.internal.remote.internal.inet.SocketConnection$SocketOutputStream.flush(SocketConnection.java:261)        at org.gradle.internal.remote.internal.inet.SocketConnection.flush(SocketConnection.java:138)
        ... 6 more

> Task :test

DriverStationAllianceTest > test00() SKIPPED

> Task :test FAILED

A snippet of the error log:

---------------  T H R E A D  ---------------

Current thread (0x000001e653bf3750):  JavaThread "Test worker" [_thread_in_native, id=8896, stack(0x00000054b8900000,0x00000054b8a00000)]

Stack: [0x00000054b8900000,0x00000054b8a00000],  sp=0x00000054b89fb920,  free space=1006k
Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)
C  [wpiHal.dll+0x41c8]

Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
j  edu.wpi.first.hal.simulation.DriverStationDataJNI.setAllianceStationId(I)V+0
j  edu.wpi.first.wpilibj.simulation.DriverStationSim.setAllianceStationId(Ledu/wpi/first/hal/AllianceStationID;)V+80
j  DriverStationAllianceTest.test00()V+3
v  ~StubRoutines::call_stub
... much more stack. ..

Open an issue in Github, or am I doing something wrong?

You need to add a HAL.initialize(500, 0); to the start of the test to make sure the HAL is initialized before you try to use it.

1 Like

Thanks; took care of the crashes, and now I see the bit in the FIRST docs about unit testing.

Back to the original issue; the setAllianceStationId is not coming through:

import org.junit.jupiter.api.Test;

import edu.wpi.first.hal.AllianceStationID;
import edu.wpi.first.hal.HAL;
import edu.wpi.first.wpilibj.DriverStation;
import edu.wpi.first.wpilibj.simulation.DriverStationSim;

public class DriverStationAllianceTest {

    @Test
    public void test00() throws InterruptedException {
        HAL.initialize(500, 0);

        DriverStationSim.setAllianceStationId(AllianceStationID.Blue1);
        System.out.println (DriverStation.getAlliance() + " " + DriverStation.getLocation());


        DriverStationSim.setAllianceStationId(AllianceStationID.Red3);
        System.out.println (DriverStation.getAlliance() + " " + DriverStation.getLocation());
    }
}

results in "Red 1", "Red 1".

You also need to call DriverStationSim.notifyNewData() so the changes propagate.

2 Likes

ok, that was the last missing piece. This works great.

Thank you!

import org.junit.jupiter.api.Test;

import edu.wpi.first.hal.AllianceStationID;
import edu.wpi.first.hal.HAL;
import edu.wpi.first.wpilibj.DriverStation;
import edu.wpi.first.wpilibj.simulation.DriverStationSim;

public class DriverStationAllianceTest {

    @Test
    public void test00() throws InterruptedException {
        HAL.initialize(500, 0);

        DriverStationSim.setAllianceStationId(AllianceStationID.Blue1);
        DriverStationSim.notifyNewData();
        System.out.println (DriverStation.getAlliance() + " " + DriverStation.getLocation());

        DriverStationSim.setAllianceStationId(AllianceStationID.Red3);
        DriverStationSim.notifyNewData();
        System.out.println (DriverStation.getAlliance() + " " + DriverStation.getLocation());
    }
}
```