# \[Need Help\] Strange behavior of WebSocket checks

**URL:** https://community.gatling.io/t/need-help-strange-behavior-of-websocket-checks/7080
**Category:** Gatling (Open-Source)
**Created:** [August 18, 2022, 1:26pm UTC](https://community.gatling.io/t/need-help-strange-behavior-of-websocket-checks/7080 "2022-08-18T13:26:38Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![nobody](https://avatars.discourse-cdn.com/v4/letter/n/94ad74/32.png) [@nobody](https://community.gatling.io/u/nobody)
#### Post date: [August 18, 2022, 1:26pm UTC](https://community.gatling.io/t/need-help-strange-behavior-of-websocket-checks/7080/1 "2022-08-18T13:26:38Z")

</div>

Hi,

I’m testing a websocket service, the scenario should be:

- Each client keeps the connection open and sends 1 message with a random unique sequence number every 10 seconds.
- Server returns the seq in response body.
- Client should only accept the message with the same seq.

I also want to print the seq for failed requests.  
Here’s my code:

```java
public class WebsocketTest extends Simulation {
    private static final Logger log = LoggerFactory.getLogger(WebsocketTest.class);

    // ...

    HttpProtocolBuilder httpConf = http
        .wsBaseUrl(baseUrl)
        .wsAutoReplySocketIo4();

    ScenarioBuilder scn = scenario("WebSocket")
        .exec(ws("Connect WS").connect(path))
        .exitHereIfFailed()
        .repeat(loop).on(
            pause(sendIntervalMin, sendIntervalMax)
                .exec(session -> {
                    session.markAsSucceeded();
                    String nanoTime = String.valueOf(System.nanoTime());
                    return session.set("seq", System.currentTimeMillis() + nanoTime.substring(nanoTime.length() - 5));
                })
                .exec(ws("Send text")
                    .sendText(data)
                    .await(responseTimeout).on(
                        ws.checkTextMessage("Check response code")
                            .matching(
                                jsonPath(matchJsonPath).ofLong().isEL("#{seq}").saveAs("returnSeq")
                            )
                            .check(
                                jsonPath(checkJsonPath).ofInt().is(returnCode).saveAs("returnCode")
                            )
                    )
                ).doIf(Session::isFailed).then(
                    exec(session -> {
                            log.error("Failed request seq: {} ; Return seq: {} ; Return code: {} ; session: {}",
                                session.getString("seq"),
                                session.getString("returnSeq"),
                                session.getString("returnCode"),
                                session.userId()
                            );

                            return session.reset();
                        }
                    )
                )
        ).exec(ws("Close WS").close());

    public WebsocketTest() {
        setUp(
            scn.injectOpen(rampUsers(connections).during(rampUp))
        ).protocols(httpConf);
    }
}

```

But the result is so confusing. In this pic, validation is passed, but an error msg is still logged.  
So `Session::isFailed` returns true?

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/gatling2/original/1X/d762de99f6856876982aa07554d0d6257d55086b.jpeg)

And here looks like thread gatling-1-2 consumes gatling-1-3’s msg, then gatling-1-3 has to wait for nothing until timeout and fails the request.

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/gatling2/original/1X/acfa7a819112b2d040de6ff6806c7fffd1f5092f.jpeg)  
 ![image](https://europe1.discourse-cdn.com/flex013/uploads/gatling2/original/1X/f78d4e4e87ec75ec15ab2584bd69b88ffb14724d.png)

Env:

- Gradle 7.5 w/ Gatling plugin 3.8.3.2
- JDK 11
- macOS 12.1

---

<div class="post-metadata">

### Author: ![slandelle](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/slandelle/32/4_2.png) [@slandelle](https://community.gatling.io/u/slandelle)
#### Post date: [August 18, 2022, 1:45pm UTC](https://community.gatling.io/t/need-help-strange-behavior-of-websocket-checks/7080/4 "2022-08-18T13:45:22Z")

</div>

> [@nobody](#):
>
> ```auto
> session -> {
> session.markAsSucceeded();
> String nanoTime = String.valueOf(System.nanoTime());
> 
> ```

Session is immutable: [Gatling - Session API](https://gatling.io/docs/gatling/reference/current/core/session/session_api/#setting-attributes).

---

<div class="post-metadata">

### Author: ![nobody](https://avatars.discourse-cdn.com/v4/letter/n/94ad74/32.png) [@nobody](https://community.gatling.io/u/nobody)
#### Post date: [August 18, 2022, 1:57pm UTC](https://community.gatling.io/t/need-help-strange-behavior-of-websocket-checks/7080/5 "2022-08-18T13:57:50Z")

</div>

Thank you slandelle. The 1st problem is solved, but the 2nd one still here.

New code:

```java
.exec(session -> {
    Session newSession = session.markAsSucceeded();
    String nanoTime = String.valueOf(System.nanoTime());
    return newSession.set("seq", System.currentTimeMillis() + nanoTime.substring(nanoTime.length() - 5));
})

```

I got 12 out of 3500 requests being mistakenly treated as non-matching:

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/gatling2/original/2X/b/bbdd0e5a51719ea75d343df5fd9768bc68c9f7d3.png)  
 ![image](https://europe1.discourse-cdn.com/flex013/uploads/gatling2/original/2X/8/81053678918a0f561e618ec130a829d3ceb40367.png)

```auto
================================================================================
2022-08-18 21:51:01 121s elapsed
---- Requests ------------------------------------------------------------------
> Global (OK=8388 KO=12 )
> Connect WS (OK=700 KO=0 )
> Send text (OK=3500 KO=0 )
> Check response code (OK=3488 KO=12 )
> Close WS (OK=700 KO=0 )
---- Errors --------------------------------------------------------------------
> Check Check response code timeout 12 (100.0%)

---- WebSocket -----------------------------------------------------------------
[##########################################################################]100%
          waiting: 0 / active: 0 / done: 700   
================================================================================

```

---

<div class="post-metadata">

### Author: ![slandelle](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/slandelle/32/4_2.png) [@slandelle](https://community.gatling.io/u/slandelle)
#### Post date: [August 18, 2022, 2:25pm UTC](https://community.gatling.io/t/need-help-strange-behavior-of-websocket-checks/7080/6 "2022-08-18T14:25:15Z")

</div>

Your `matchJsonPath` (missing from your sample) is likely wrong.

```java
.matching(
   jsonPath(matchJsonPath).ofLong().isEL("#{seq}").saveAs("returnSeq")
)

```

Note that saveAs here won’t work (in matching).

---

<div class="post-metadata">

### Author: ![nobody](https://avatars.discourse-cdn.com/v4/letter/n/94ad74/32.png) [@nobody](https://community.gatling.io/u/nobody)
#### Post date: [August 18, 2022, 2:35pm UTC](https://community.gatling.io/t/need-help-strange-behavior-of-websocket-checks/7080/7 "2022-08-18T14:35:30Z")

</div>

`matchJsonPath` is using the default value and unchanged during the test.  
If the jsonpath is wrong, all requests should fail.

```java
String matchJsonPath = System.getProperty("matchJsonPath", "$.body.seq");

```

The response body is fixed (except client seq(`$.body.seq`), server seq(`$.seq`) and timestamp), so `$.body.seq` is always available.

```json
{"body":{"code":10001,"desc": "UNSUPPORT PROTOCOL","ptype":"UNRECOGNIZED","seq":166083059168627347},"ptype":"ACK","seq":1660830593473,"timestamp":1660830593474, "ver":"1.0.0"}

```

---

<div class="post-metadata">

### Author: ![nobody](https://avatars.discourse-cdn.com/v4/letter/n/94ad74/32.png) [@nobody](https://community.gatling.io/u/nobody)
#### Post date: [August 18, 2022, 2:42pm UTC](https://community.gatling.io/t/need-help-strange-behavior-of-websocket-checks/7080/8 "2022-08-18T14:42:42Z")

</div>

Under higher concurrency, more and more responses seemed to fail to match the “seq” attribute in Session?

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/gatling2/original/2X/7/7a59dd321b5722f146b7c54ce0621e2b741e4655.png)

---

<div class="post-metadata">

### Author: ![slandelle](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/slandelle/32/4_2.png) [@slandelle](https://community.gatling.io/u/slandelle)
#### Post date: [August 18, 2022, 2:50pm UTC](https://community.gatling.io/t/need-help-strange-behavior-of-websocket-checks/7080/9 "2022-08-18T14:50:18Z")

</div>

> session.set(“seq”, System.currentTimeMillis() + nanoTime.substring(nanoTime.length() - 5));

“seq” is a String here.

> jsonPath(matchJsonPath).ofLong().isEL(“#{seq}”).saveAs(“returnSeq”)

And here you’re comparing a long and a String, so it’s bound to fail.

---

<div class="post-metadata">

### Author: ![slandelle](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/slandelle/32/4_2.png) [@slandelle](https://community.gatling.io/u/slandelle)
#### Post date: [August 18, 2022, 2:51pm UTC](https://community.gatling.io/t/need-help-strange-behavior-of-websocket-checks/7080/10 "2022-08-18T14:51:01Z")

</div>

Then, if you face an issue, please provide a reproducer as instructed here: [How to Ask a Question](https://community.gatling.io/t/how-to-ask-a-question/6538).

---

<div class="post-metadata">

### Author: ![nobody](https://avatars.discourse-cdn.com/v4/letter/n/94ad74/32.png) [@nobody](https://community.gatling.io/u/nobody)
#### Post date: [August 24, 2022, 6:25am UTC](https://community.gatling.io/t/need-help-strange-behavior-of-websocket-checks/7080/11 "2022-08-24T06:25:23Z")

</div>

> And here you’re comparing a long and a String, so it’s bound to fail.

I found out the root cause, nothing to do with test code or gatling.  
There was a bug in our server code, in some circumstances, server sends msg to a wrong channel.
