mercredi 28 avril 2021

If else loop is skipping if clause [if(2 > 1) ] and moving straight to dead code in the else clause. gRPC instantiation

I'm in the midst of the crunchier end of a gRPC project and I have no idea what is going wrong in this rpc. I am testing the RPC with Bloom and it is continually just executing the final clause and skipping the first one. Had thought it was a gRPC issue but after a lot of editing I tried changing the loop to a guaranteed true just to ensure it was executable and it continued to skip over it.

I am relatively new so I am sure I made some terrible design choices but I can't see anything catastrophically bad. Project was created with Maven for what it is worth

@SuppressWarnings("static-access")
@Override
    public void mWClock(targetRoom request, StreamObserver<lockStatus> responseObserver) {
    int key = request.getTarget();
    int trigger = 0;
    //lockStatus response;//null added due to promp to initialize
    //lockStatus response2;
    if (key == 0) {
        //success
        do {
            CapacityService.MaleWC().newBuilder()//These are throwing up warnings. Unsure how to address
            .setPopulation(randomInt(5));//randomly simulate the movement of people into the room
            try {
                Thread.sleep(30000);//5 minute intervals, reduce for quick demo
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }catch (OutOfMemoryError e) {//Using thread indefinitely could eventually cause memory issues
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
            //THIS WAS THE ORIGINAL COMPARISON BEFORE CHANGING TO 2 > 1
            //if(CapacityService.MaleWC().getPopulation() >= CapacityService.MaleWC().getCapacity()) {
            if(2 > 1) {
                CapacityService.MaleWC().newBuilder()
                .setLock(true);
                trigger++;
                //
                String result1 = "Male Bathroom is currently at or over capacity, please wait";
                lockStatus response = lockStatus.newBuilder()
                        .setStatus(result1)
                        .setSpaces(0)
                        .build();
                responseObserver.onNext(response);
                
                
                try {
                    Thread.sleep(30000);Just to delay returns and simulate movement. 
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }catch (OutOfMemoryError e) {//Using thread indefinitely will eventually cause memory issues
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                CapacityService.MaleWC().newBuilder()
                .setPopulation(randomInt(3));//To reduce it below capacity so the loop continues at while loop
                
            }else {
                int space = CapacityService.MaleWC().getCapacity() - CapacityService.MaleWC().getPopulation();
                if(space < 0) {
                    space = 0;//Ensure no negative value for return
                }
                trigger++;
                String result2 = "Male Bathroom is currently available for use";
                lockStatus response = lockStatus.newBuilder()
                        //response.newBuilder()
                        .setStatus(result2)
                        .setSpaces(0)
                        .build();
                responseObserver.onNext(response);
                
                
            }
            //end of do loop
        }while(trigger < 3000);
        responseObserver.onCompleted();
        
        

        
    }else {
        //failure, wrong number entered. TO DO
        responseObserver.onCompleted();
    }

}

The MaleWC object is a message from a different services proto being imported here and instantiated at the top. What may possibly be of note is that I was unable to import the proto file where the Room message was created into the proto file with the MWClock service but I don't know if that affects anything. I'm also not overly sure if the static access warning could be causing the issue

@SuppressWarnings("static-access")
@Override
    public static Room MaleWC() {
    return Room.newBuilder()
            .setCapacity(3)
            .setPopulation(0)
            .setLock(false)
            .build();
}

The server is definitely working from what I can tell. It just keeps jumping to the else clause. Can anyone shed some light? Can share the proto file for the service if it's needed but I'm guessing the proto is fine as it's returning the correct stuff from the else statement, unless the issue was caused by the importing snaffoo mentioned earlier.

Aucun commentaire:

Enregistrer un commentaire