summaryrefslogtreecommitdiff
path: root/i/simulotron/src/hub/test_hub.cc
diff options
context:
space:
mode:
Diffstat (limited to 'i/simulotron/src/hub/test_hub.cc')
-rw-r--r--i/simulotron/src/hub/test_hub.cc162
1 files changed, 162 insertions, 0 deletions
diff --git a/i/simulotron/src/hub/test_hub.cc b/i/simulotron/src/hub/test_hub.cc
new file mode 100644
index 0000000..c9645d0
--- /dev/null
+++ b/i/simulotron/src/hub/test_hub.cc
@@ -0,0 +1,162 @@
+/* test_hub.cc - Programme de test du hub. */
+/* Simulotron - Programme de simulation de robot {{{
+ *
+ * Copyright (C) 2006 Nicolas Haller
+ *
+ * Robot APB Team/Efrei 2005.
+ * Web: http://assos.efrei.fr/robot/
+ * Email: robot AT efrei DOT fr
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * }}} */
+
+#include "comc/struct_message.hh"
+#include "hub/hub.hh"
+#include "socket/socket_server.hh"
+#include "comc/comc.hh"
+
+#include <string>
+#include <iostream>
+#include <sys/wait.h>
+
+// message de test
+msg0 ms1, ms2;
+
+int testHub(Hub & hub);
+void testModule1(int pauseMode1);
+void testModule2(int pauseMode2);
+
+int main(void)
+{
+ //pauseMode pour debbuger le fork
+ int pauseMode1 = 0;
+ int pauseMode2 = 0;
+ pid_t pidModule1, pidModule2;
+ //retour des test
+ int resultHub, resultModule1, resultModule2;
+ //Initialisation des valeurs de la struct de test
+ ms1.i = 42;
+ ms1.d = 23.23;
+ ms1.strh = "chier!!!";
+ ms1.s = 4;
+ ms1.str = "Ho!! Une structure de test, comme c'est amusant!!";
+ ms1.l = 3432;
+ // Création du hub
+ Hub hub(std::string(), 4242);
+ // On crée le fork du module1
+ pidModule1 = fork();
+ if (pidModule1 == 0) // processus fils
+ testModule1(pauseMode1);
+ else //processus père
+ {
+ pidModule2 = fork();
+ if (pidModule2 == 0)
+ testModule2(pauseMode2);
+ else
+ resultHub = testHub(hub);
+
+ // On analyse les résultats
+ waitpid(pidModule1, &resultModule1, 0);
+ waitpid(pidModule2, &resultModule2, 0);
+ if (!(WIFEXITED(resultModule1) && WEXITSTATUS(resultModule1) == 0))
+ {
+ std::cerr << "ECHEC: Problème coté module 1" << std::endl;
+ exit (-1);
+ }
+ if (!(WIFEXITED(resultModule2) && WEXITSTATUS(resultModule2) == 0))
+ {
+ std::cerr << "ECHEC: Problème coté module 2" << std::endl;
+ exit (-1);
+ }
+ if (resultHub != 0)
+ {
+ std::cerr << "ECHEC: Problème coté hub" << std::endl;
+ exit (-1);
+ }
+ }
+ return 0;
+}
+
+int testHub(Hub & hub)
+{
+ try
+ {
+ hub.start();
+ }
+ catch (std::exception & c)
+ {
+ std::cout << "Oops, exeption dans le hub" << std::endl;
+ std::cout << c.what() << std::endl;
+ }
+ return 0;
+}
+
+void testModule1(int pauseMode)
+{
+ while(pauseMode)
+ sleep (10);
+ try
+ {
+ // Création du comc
+ ComC comc(std::string("127.0.0.1"), 4242, "module1");
+ // Initialisation de la com avec le hub
+ comc.initHub();
+ comc.send(ms1, std::string("module2"));
+ }
+ catch(std::exception & chier)
+ {
+ std::cout << "CHIER !! Une exception a été lancé coté module1!!" << std::endl;
+ std::cout << chier.what() << std::endl;
+ exit (1);
+ }
+ exit (0);
+}
+
+void testModule2(int pauseMode)
+{
+ while(pauseMode)
+ sleep (10);
+ try
+ {
+ GSMessage gsm;
+ std::string source, dest;
+ int msgId;
+ // Création du comc
+ ComC comc(std::string("127.0.0.1"), 4242, "module2");
+ // Initialisation de la com avec le hub
+ comc.initHub();
+ // Reception du message
+ comc.receiveGS(gsm, source, dest, msgId, true);
+ ms2.gsToMsg(gsm);
+ // Test dessus
+ if (ms1.i != ms2.i ||
+ ms1.d != ms2.d ||
+ ms1.strh != ms2.strh ||
+ ms1.s != ms2.s ||
+ ms1.str != ms2.str ||
+ ms1.l != ms2.l)
+ {
+ std::cerr << "CHIER!! Le message n'est pas identique à l'arrivé!!" << std::endl;
+ }
+ }
+ catch(std::exception & chier)
+ {
+ std::cout << "CHIER !! Une exception a été lancé coté module2!!" << std::endl;
+ std::cout << chier.what() << std::endl;
+ exit (1);
+ }
+ exit (0);
+}