changeset 1104:0cdfcf4df1c6

PgSqlFlowHandlerTest - more tests
author Devel 2
date Fri, 15 May 2020 08:49:42 +0200
parents 4b9c98988fa4
children 48296cc7088e
files stress-tester/src/main/java/com/passus/st/client/DbMetric.java stress-tester/src/main/java/com/passus/st/client/pgsql/PgSqlFlowHandler.java stress-tester/src/test/java/com/passus/st/client/pgsql/PgSqlFlowHandlerTest.java
diffstat 3 files changed, 44 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/client/DbMetric.java	Wed May 13 15:09:08 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/client/DbMetric.java	Fri May 15 08:49:42 2020 +0200
@@ -32,6 +32,10 @@
         }
     }
 
+    public int getRecordsNum() {
+        return recordsNum.intValue();
+    }
+
     public void incRecordsNum() {
         recordsNum.increment();
     }
--- a/stress-tester/src/main/java/com/passus/st/client/pgsql/PgSqlFlowHandler.java	Wed May 13 15:09:08 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/client/pgsql/PgSqlFlowHandler.java	Fri May 15 08:49:42 2020 +0200
@@ -45,6 +45,10 @@
         return NETFLOW;
     }
 
+    PgSqlMetric getMetric() {
+        return metric;
+    }
+
     @Override
     protected PgSqlMetric createMetric() {
         return new PgSqlMetric();
@@ -116,6 +120,16 @@
 
                     disconnectAndBlock(flowContext);
                 }
+            } else {
+                for (PgSqlMessage packet : bulk.packets) {
+                    if (packet.getType() == PgSqlMessageType.DATA_ROW) {
+                        if (collectMetrics) {
+                            synchronized (metric) {
+                                metric.incRecordsNum();
+                            }
+                        }
+                    }
+                }
             }
         }
     }
--- a/stress-tester/src/test/java/com/passus/st/client/pgsql/PgSqlFlowHandlerTest.java	Wed May 13 15:09:08 2020 +0200
+++ b/stress-tester/src/test/java/com/passus/st/client/pgsql/PgSqlFlowHandlerTest.java	Fri May 15 08:49:42 2020 +0200
@@ -1,6 +1,7 @@
 package com.passus.st.client.pgsql;
 
 import com.passus.data.ByteBuff;
+import com.passus.net.packet.Packet;
 import com.passus.net.packet.Tcp;
 import com.passus.net.pgsql.PgSqlDecoder;
 import com.passus.net.pgsql.PgSqlMessage;
@@ -12,8 +13,7 @@
 import org.testng.annotations.Test;
 
 import java.text.ParseException;
-import java.util.Iterator;
-import java.util.List;
+import java.util.*;
 import java.util.stream.Collectors;
 
 import static org.testng.AssertJUnit.*;
@@ -31,8 +31,14 @@
     }
 
     public Result processReqResp(String pcapFile) {
+        return processReqResp(pcapFile, Collections.EMPTY_MAP);
+    }
+
+    public Result processReqResp(String pcapFile, Map<String, Object> params) {
+        int initStage = (int) params.getOrDefault("initStage", PgSqlFlowContext.STAGE_NONE);
+
         List<Tcp> tcps = PcapUtils.readTcpPackets(pcapFile);
-        tcps = tcps.stream().filter(t -> t.hasPayload()).collect(Collectors.toList());
+        tcps = tcps.stream().filter(Packet::hasPayload).collect(Collectors.toList());
 
         Iterator<Tcp> it = tcps.iterator();
         FlowContext flowContext = new FlowContext(info);
@@ -43,6 +49,7 @@
 
         PgSqlFlowHandler handler = new PgSqlFlowHandler();
         handler.init(flowContext);
+        handler.context.stage = initStage;
         handler.onRequestSent(reqDecoder.getResult(), flowContext);
         int afterReqStage = handler.context.stage;
 
@@ -52,9 +59,9 @@
             ByteBuff buff = ByteBuff.wrap(payload, 0, payload.length);
             decoder.decode(buff, flowContext);
             if (it.hasNext()) {
-                assertEquals(PgSqlDecoder.STATE_DATA_NEEDED, reqDecoder.state());
+                assertEquals(PgSqlDecoder.STATE_DATA_NEEDED, decoder.state());
             } else {
-                assertEquals(PgSqlDecoder.STATE_FINISHED, reqDecoder.state());
+                assertEquals(PgSqlDecoder.STATE_FINISHED, decoder.state());
             }
         }
 
@@ -82,6 +89,20 @@
         assertEquals(PgSqlFlowContext.STAGE_STARTUP_MESSAGE, result.afterRequestStage);
     }
 
+    @Test
+    public void testProcessSimpleQueryAndData() {
+        Map<String, Object> params = new HashMap<>();
+        params.put("initStage", PgSqlFlowContext.STAGE_AUTH_OK);
+        Result result = processReqResp("pcap/pgsql/pgsql_simple_query_and_data.pcap", params);
+
+        assertEquals(93, result.bulk.packets.size());
+        assertEquals(PgSqlFlowContext.STAGE_AUTH_OK, result.afterRequestStage);
+
+        PgSqlMetric metric = result.handler.getMetric();
+        assertEquals(90, metric.getRecordsNum());
+
+    }
+
     private class Result {
 
         final PgSqlFlowHandler handler;