librelist archives

« back to archive

[PATCH] FIX: MsgSeqNum lower than expected test is added

[PATCH] FIX: MsgSeqNum lower than expected test is added

From:
Marat Stanichenko
Date:
2014-01-30 @ 19:55
Signed-off-by: Marat Stanichenko <mstanichenko@gmail.com>
---
 tools/fix/fix_client.c                          |   50 +++++++++++++++++++----
 tools/fix/fix_server.c                          |    5 +++
 tools/fix/scripts/2c_MsgSeqNumLowerThanExpected |    2 +
 tools/fix/test.c                                |    1 +
 tools/test.py                                   |    1 +
 5 files changed, 52 insertions(+), 7 deletions(-)
 create mode 100644 tools/fix/scripts/2c_MsgSeqNumLowerThanExpected

diff --git a/tools/fix/fix_client.c b/tools/fix/fix_client.c
index 25ed122..a46af2a 100644
--- a/tools/fix/fix_client.c
+++ b/tools/fix/fix_client.c
@@ -50,6 +50,43 @@ static void signal_handler(int signum)
 		stop = true;
 }
 
+static int fix_logout_send(struct fix_session *session, const char *text)
+{
+	struct fix_field fields[] = {
+		FIX_STRING_FIELD(Text, text),
+	};
+	long nr_fields = ARRAY_SIZE(fields);
+	struct fix_message logout_msg;
+
+	if (!text)
+		nr_fields--;
+
+	logout_msg	= (struct fix_message) {
+		.type		= FIX_MSG_TYPE_LOGOUT,
+		.nr_fields	= nr_fields,
+		.fields		= fields,
+	};
+
+	return fix_session_send(session, &logout_msg, 0);
+}
+
+static int fix_client_logout(struct fix_session *session, const char 
*text, bool grace)
+{
+	int ret;
+
+	if (grace)
+		ret = fix_session_logout(session, text);
+	else
+		ret = fix_logout_send(session, text);
+
+	if (ret)
+		fprintf(stderr, "Client Logout FAILED\n");
+	else
+		fprintf(stdout, "Client Logout OK\n");
+
+	return ret;
+}
+
 static int fix_client_script(struct fix_session_cfg *cfg, struct 
fix_client_arg *arg)
 {
 	struct fcontainer *s_container = NULL;
@@ -132,6 +169,11 @@ retry:
 			break;
 		}
 
+		if (fix_message_type_is(msg, FIX_MSG_TYPE_LOGOUT)) {
+			ret = fix_client_logout(session, NULL, false);
+			goto exit;
+		}
+
 next:
 		expected_elem = next_elem(s_container);
 		tosend_elem = next_elem(c_container);
@@ -141,13 +183,7 @@ next:
 	if (ret)
 		goto exit;
 
-	ret = fix_session_logout(session, NULL);
-	if (ret) {
-		fprintf(stderr, "Client Logout FAILED\n");
-		goto exit;
-	}
-
-	fprintf(stdout, "Client Logout OK\n");
+	ret = fix_client_logout(session, NULL, true);
 
 exit:
 	fcontainer_free(c_container);
diff --git a/tools/fix/fix_server.c b/tools/fix/fix_server.c
index 3e2f495..893a4b6 100644
--- a/tools/fix/fix_server.c
+++ b/tools/fix/fix_server.c
@@ -50,6 +50,7 @@ retry:
 	};
 
 	fix_session_send(session, &logon_msg, 0);
+	session->active = true;
 
 	return true;
 }
@@ -61,6 +62,9 @@ static bool fix_server_logout(struct fix_session *session)
 	bool ret = true;
 
 retry:
+	if (!session->active)
+		goto exit;
+
 	msg = fix_session_recv(session, 0);
 	if (!msg)
 		goto retry;
@@ -74,6 +78,7 @@ retry:
 
 	fix_session_send(session, &logout_msg, 0);
 
+exit:
 	return ret;
 }
 
diff --git a/tools/fix/scripts/2c_MsgSeqNumLowerThanExpected 
b/tools/fix/scripts/2c_MsgSeqNumLowerThanExpected
new file mode 100644
index 0000000..9c7c1d7
--- /dev/null
+++ b/tools/fix/scripts/2c_MsgSeqNumLowerThanExpected
@@ -0,0 +1,2 @@
+c35=034=1
+s35=558=MsgSeqNum too low, expecting 2 received 1
diff --git a/tools/fix/test.c b/tools/fix/test.c
index 6eade60..fd1a263 100644
--- a/tools/fix/test.c
+++ b/tools/fix/test.c
@@ -94,6 +94,7 @@ int init_elem(struct felem *elem, char *line)
 			case GapFillFlag:
 			case SendingTime:
 			case TestReqID:
+			case Text:
 				elem->msg.fields[elem->msg.nr_fields++] = FIX_STRING_FIELD(tag, end);
 				break;
 			case MsgSeqNum:
diff --git a/tools/test.py b/tools/test.py
index d6f36f9..026864a 100755
--- a/tools/test.py
+++ b/tools/test.py
@@ -14,6 +14,7 @@ fix_tests = [
   # details.
   #
   "2b_MsgSeqNumHigherThanExpected",
+  "2c_MsgSeqNumLowerThanExpected",
   "4b_TestRequestMessageIsReceived"
 ]
 
-- 
1.7.9.5