User story RICPLT-2620
[ric-app/admin.git] / test / unit_test_xapp.cc
index 6801ec2..e1d47f1 100644 (file)
@@ -27,7 +27,6 @@
 #include <unistd.h>
 #include <xapp_utils.hpp>
 
-#define RMR_RT_FILE "/home/asridharan/projects/ric-xapp-dev/ric-app/adm-ctrl-xapp/test/uta_rtg.rt"
 #define MESSAGE_SIZE 512
 
 int num_recv_pkts = 0;
@@ -62,13 +61,20 @@ void dropped_pkts(rmr_mbuf_t *send_msg){
   num_dropped_pkts++;
 }
 
-bool ping_x(rmr_mbuf_t *rcv_msg){
-  rcv_msg->mtype = 101; //pong
+bool pong_a1(rmr_mbuf_t *rcv_msg){
+  rcv_msg->mtype = A1_POLICY_RESP; 
   num_ping_pkts++;
   return true;
 }
 
+
 bool pong_x(rmr_mbuf_t *rcv_msg){
+  rcv_msg->mtype = 102; //ping port
+  num_ping_pkts++;
+  return true;
+}
+
+bool ping_recv(rmr_mbuf_t * rcv_msg){
   num_pong_pkts++;
   return false;
 }
@@ -79,39 +85,25 @@ TEST_CASE("Test xapp functionality", "[xapp]"){
   // Test parameters
   char app_name[128] = "Test App";
   char port[16] = "tcp:4999";
-  int num_retries = 4;
 
+  init_logger("UNIT_TEST_XAPP", MDCLOG_INFO);
   
-  mdclog_attr_t *attr;
-  mdclog_attr_init(&attr);
-  mdclog_attr_set_ident(attr, "UNIT TEST XAPP FRAMEWORK");
-  mdclog_init(attr);
-  mdclog_level_set(MDCLOG_INFO);
-  mdclog_attr_destroy(attr);
-
-  SECTION("RMR illegal options"){
-    char illegal_port [] = "udp:-1";
-    REQUIRE_THROWS(XaPP(app_name, illegal_port, sizeof(Test_message), 1));
-    REQUIRE_THROWS(XaPP(app_name, port, RMR_BUFFER_SIZE + 1, 1));
+  SECTION("Illegal buffer size"){
+    REQUIRE_THROWS(XaPP(app_name, port, RMR_BUFFER_SIZE + 1));
   }
-
+  
   SECTION("All good"){
-    REQUIRE_NOTHROW(XaPP(app_name, port, sizeof(Test_message), 1));
+    REQUIRE_NOTHROW(XaPP(app_name, port, sizeof(Test_message)));
   }
    
   SECTION("Simple + memory"){
-    XaPP check_xapp = XaPP(app_name, port, sizeof(Test_message), 1);
-    REQUIRE(check_xapp.getName() == std::string(app_name));
+    XaPP check_xapp = XaPP(app_name, port, sizeof(Test_message));
+    REQUIRE(check_xapp.get_name() == std::string(app_name));
   }
   
   SECTION("Configuration test"){  
-    XaPP test_xapp = XaPP(app_name, port, sizeof(Test_message) , 1);
-    test_xapp.set_num_retries(num_retries);
-    
-    REQUIRE(test_xapp.get_num_retries() == num_retries); 
-    REQUIRE(test_xapp.getStatus() == 1);
-    REQUIRE(test_xapp.get_num_retries() != 2);
-    REQUIRE(test_xapp.getStatus() == true);
+    XaPP test_xapp = XaPP(app_name, port, sizeof(Test_message));
+    REQUIRE(test_xapp.get_status() == true);
     REQUIRE(test_xapp.get_rmr_context() != NULL);
     
   }
@@ -123,11 +115,10 @@ TEST_CASE("Test xapp functionality", "[xapp]"){
     failed_tx = 0;
     
     // Instantiate and configure xAPP
-    XaPP test_xapp = XaPP(app_name, port, sizeof(Test_message) , 1);
-    test_xapp.set_num_retries(num_retries);
+    XaPP test_xapp = XaPP(app_name, port, sizeof(Test_message));
 
     // Start receiver for test
-    test_xapp.Start(&rcvd_pkts);
+    test_xapp.StartThread(rcvd_pkts);
     sleep(1);
   
     // Test Send  normal message
@@ -136,7 +127,7 @@ TEST_CASE("Test xapp functionality", "[xapp]"){
     for(i = 0; i < NumPkts; i++){
       clock_gettime(CLOCK_REALTIME, &(my_message.ts));
       snprintf(my_message.payload, MESSAGE_SIZE, "hello world %d", i);
-      bool res = test_xapp.Send(0, sizeof(Test_message), (void *) (&my_message));
+      bool res = test_xapp.Send(102, sizeof(Test_message), (void *) (&my_message));
       if (!res){
        failed_tx ++;
       }
@@ -144,10 +135,12 @@ TEST_CASE("Test xapp functionality", "[xapp]"){
     
     }
     sleep(1);
-
-    test_xapp.Stop();  
+    REQUIRE(test_xapp.get_num_active_threads() == 1);
+    test_xapp.Stop();
+    REQUIRE(test_xapp.get_num_active_threads() == 0);
+    
     std::cout <<"Num Packets Sent = " << NumPkts << " Received packets = " << num_recv_pkts << " Dropped packets = " << num_dropped_pkts << " Failed sends = "<< failed_tx << std::endl;
-    std::cout <<"Num attempts = " << test_xapp.get_Send_attempts() <<  " Num failed = " << test_xapp.get_Send_fails() << std::endl;
+
     
     REQUIRE(num_recv_pkts > 0);
     REQUIRE(num_dropped_pkts == 0);
@@ -155,229 +148,222 @@ TEST_CASE("Test xapp functionality", "[xapp]"){
     REQUIRE(num_recv_pkts == NumPkts);
   }
 
-//   SECTION("Transmission test with start specific thread"){
-//     num_recv_pkts = 0;
-//     num_dropped_pkts = 0;
-//     failed_tx = 0;
-    
-//     // Instantiate and configure xAPP
-//     XaPP test_xapp = XaPP(app_name, port, sizeof(Test_message) , 1);
-//     test_xapp.set_num_retries(num_retries);
-    
-//     // Start receiver for test
-//     test_xapp.StartThread(&rcvd_pkts);
-//     sleep(1);
-  
-//     // Test Send  normal message
-//     Test_message my_message;
-//     uint32_t i = 0;
-//     for(i = 0; i < NumPkts; i++){
-//       clock_gettime(CLOCK_REALTIME, &(my_message.ts));
-//       snprintf(my_message.payload, MESSAGE_SIZE, "hello world %d", i);
-//       bool res = test_xapp.Send(0, sizeof(Test_message), (void *) (&my_message));
-//       if (!res){
-//     failed_tx ++;
-//       }
-//       usleep(10);
-    
-//     }
-//     sleep(1);
-
-//     test_xapp.Stop();  
-//     std::cout <<"Num Packets Sent = " << NumPkts << " Received packets = " << num_recv_pkts << " Dropped packets = " << num_dropped_pkts << " Failed sends = "<< failed_tx << std::endl;
-//     std::cout <<"Num attempts = " << test_xapp.get_Send_attempts() <<  " Num failed = " << test_xapp.get_Send_fails() << std::endl;
-    
-//     REQUIRE(num_recv_pkts > 0);
-//     REQUIRE(num_dropped_pkts == 0);
-//     REQUIRE(failed_tx == 0);
-//     REQUIRE(num_recv_pkts == NumPkts);
-//   }
+    SECTION("Transmission test error handler with start"){
 
-//     SECTION("Transmission test error handler with start"){
-
-//       num_recv_pkts = 0;
-//       num_dropped_pkts = 0;
-//       failed_tx = 0;
+      num_recv_pkts = 0;
+      num_dropped_pkts = 0;
+      failed_tx = 0;
       
-//       // Instantiate and configure xAPP
-//       XaPP test_xapp = XaPP(app_name, port, sizeof(Test_message) , 1);
-//       test_xapp.set_num_retries(num_retries);
+      // Instantiate and configure xAPP
+      XaPP test_xapp = XaPP(app_name, port, sizeof(Test_message) );
+
       
-//       // Start receiver for test
-//       test_xapp.Start(&echo_into_space_pkts, &dropped_pkts);
-//       sleep(1);
+      // Start receiver for test
+      test_xapp.StartThread(&echo_into_space_pkts, &dropped_pkts);
+      sleep(1);
       
-//       // Test Send  normal message
-//       Test_message my_message;
-//       uint32_t i = 0;
-//       for(i = 0; i < NumPkts; i++){
-//     clock_gettime(CLOCK_REALTIME, &(my_message.ts));
-//     snprintf(my_message.payload, MESSAGE_SIZE, "hello world %d", i);
-//     bool res = test_xapp.Send(0, sizeof(Test_message), (void *) (&my_message));
-//     if (!res){
-//       failed_tx ++;
-//     }
-//     usleep(10);
+      // Test Send  normal message
+      Test_message my_message;
+      uint32_t i = 0;
+      for(i = 0; i < NumPkts; i++){
+       clock_gettime(CLOCK_REALTIME, &(my_message.ts));
+       snprintf(my_message.payload, MESSAGE_SIZE, "hello world %d", i);
+       bool res = test_xapp.Send(102, sizeof(Test_message), (void *) (&my_message));
+       if (!res){
+         failed_tx ++;
+       }
+       usleep(10);
        
-//       }
-//       sleep(1);
+      }
+      sleep(1);
       
-//       test_xapp.Stop();  
-//       std::cout <<"Num Packets Sent = " << NumPkts << " Received packets = " << num_recv_pkts << " Dropped packets = " << num_dropped_pkts << " Failed sends = "<< failed_tx << std::endl;
-//       std::cout <<"Num attempts = " << test_xapp.get_Send_attempts() <<  " Num failed = " << test_xapp.get_Send_fails() << std::endl;
+      test_xapp.Stop();  
+      std::cout <<"Num Packets Sent = " << NumPkts << " Received packets = " << num_recv_pkts << " Dropped packets = " << num_dropped_pkts << " Failed sends = "<< failed_tx << std::endl;
+
       
-//       REQUIRE(num_recv_pkts == NumPkts);
-//       REQUIRE(num_dropped_pkts == NumPkts);
-//       REQUIRE(failed_tx == 0);
-//     }
+      REQUIRE(num_recv_pkts == NumPkts);
+      REQUIRE(num_dropped_pkts == NumPkts);
+      REQUIRE(failed_tx == 0);
+    }
     
     
-//     SECTION("Transmission test error handler with start thread"){
+    SECTION("Transmission test error handler with start thread"){
       
-//       num_recv_pkts = 0;
-//       num_dropped_pkts = 0;
-//       failed_tx = 0;
+      num_recv_pkts = 0;
+      num_dropped_pkts = 0;
+      failed_tx = 0;
       
-//       // Instantiate and configure xAPP
-//       XaPP test_xapp = XaPP(app_name, port, sizeof(Test_message) , 1);
-//       test_xapp.set_num_retries(num_retries);
+      // Instantiate and configure xAPP
+      XaPP test_xapp = XaPP(app_name, port, sizeof(Test_message) );
+
       
-//       // Start receiver for test
-//       test_xapp.StartThread(&echo_into_space_pkts, &dropped_pkts);
-//       sleep(1);
+      // Start receiver for test
+      test_xapp.StartThread(&echo_into_space_pkts, &dropped_pkts);
+      sleep(1);
       
-//       // Test Send  normal message
-//       Test_message my_message;
-//       uint32_t i = 0;
-//       for(i = 0; i < NumPkts; i++){
-//     clock_gettime(CLOCK_REALTIME, &(my_message.ts));
-//     snprintf(my_message.payload, MESSAGE_SIZE, "hello world %d", i);
-//     bool res = test_xapp.Send(0, sizeof(Test_message), (void *) (&my_message));
-//     if (!res){
-//       failed_tx ++;
-//     }
-//     usleep(10);
+      // Test Send  normal message
+      Test_message my_message;
+      uint32_t i = 0;
+      for(i = 0; i < NumPkts; i++){
+       clock_gettime(CLOCK_REALTIME, &(my_message.ts));
+       snprintf(my_message.payload, MESSAGE_SIZE, "hello world %d", i);
+       bool res = test_xapp.Send(102, sizeof(Test_message), (void *) (&my_message));
+       if (!res){
+         failed_tx ++;
+       }
+       usleep(10);
        
-//       }
-//       sleep(1);
+      }
+      sleep(1);
       
-//       test_xapp.Stop();  
-//       std::cout <<"Num Packets Sent = " << NumPkts << " Received packets = " << num_recv_pkts << " Dropped packets = " << num_dropped_pkts << " Failed sends = "<< failed_tx << std::endl;
-//       std::cout <<"Num attempts = " << test_xapp.get_Send_attempts() <<  " Num failed = " << test_xapp.get_Send_fails() << std::endl;
+      test_xapp.Stop();  
+      std::cout <<"Num Packets Sent = " << NumPkts << " Received packets = " << num_recv_pkts << " Dropped packets = " << num_dropped_pkts << " Failed sends = "<< failed_tx << std::endl;
+
       
-//       REQUIRE(num_recv_pkts == NumPkts);
-//       REQUIRE(num_dropped_pkts == NumPkts);
-//       REQUIRE(failed_tx == 0);
+      REQUIRE(num_recv_pkts == NumPkts);
+      REQUIRE(num_dropped_pkts == NumPkts);
+      REQUIRE(failed_tx == 0);
 
 
-//     }
+    }
 
-//     SECTION("Test ping pong : two xapps send to each other. "){
+    SECTION("Test ping pong : two xapps send to each other. "){
 
-//       char ping_name[] = "ping";
-//       char pong_name[] = "pong";
-//       char ping_port[] = "tcp:4999";
-//       char pong_port[] = "tcp:4998";
+      char ping_name[] = "ping";
+      char pong_name[] = "pong";
+      char ping_port[] = "tcp:4999";
+      char pong_port[] = "tcp:4998";
       
-//       // Instantiate  ping xAPP
-//       XaPP ping_xapp = XaPP(ping_name, ping_port, sizeof(Test_message) , 1);
+      // Instantiate  ping xAPP
+      XaPP ping_xapp = XaPP(ping_name, ping_port, sizeof(Test_message) );
 
 
-//       // Instantiate pong xapp
-//       XaPP pong_xapp = XaPP(pong_name, pong_port, sizeof(Test_message) , 1);
+      // Instantiate pong xapp
+      XaPP pong_xapp = XaPP(pong_name, pong_port, sizeof(Test_message) );
  
-//       // Start receiver on ping
-//       ping_xapp.StartThread(ping_x);
-//       sleep(1);
+      // Start receiver on ping
+      ping_xapp.StartThread(ping_recv);
+      sleep(1);
+
+      // Start receiver on pong
+      pong_xapp.StartThread(pong_x);
+      
+      // send messages from ping to pong
+      Test_message my_message;
+      uint32_t i = 0;
+      for(i = 0; i < NumPkts; i++){
+       clock_gettime(CLOCK_REALTIME, &(my_message.ts));
+       snprintf(my_message.payload, MESSAGE_SIZE, "hello world %d", i);
+       bool res = ping_xapp.Send(101, sizeof(Test_message), (void *) (&my_message));
+       if (!res){
+         failed_tx ++;
+       }
+      }
+
+      sleep(1);
+      pong_xapp.Stop();
+
+      REQUIRE(failed_tx == 0);
+      REQUIRE(num_ping_pkts == NumPkts);
+      REQUIRE(num_pong_pkts == NumPkts);
+
+
+      // Re-run experiment but now with A1 message type when
+      // ping responds
+      pong_xapp.StartThread(pong_a1);
+      sleep(1);
 
-//       // Start receiver on pong
-//       pong_xapp.StartThread(&pong_x);
+      failed_tx = 0;
+      num_ping_pkts = 0;
+      num_pong_pkts = 0;
+
+      
+      for(i = 0; i < NumPkts; i++){
+       clock_gettime(CLOCK_REALTIME, &(my_message.ts));
+       snprintf(my_message.payload, MESSAGE_SIZE, "hello world %d", i);
+       bool res = ping_xapp.Send(101, sizeof(Test_message), (void *) (&my_message));
+       if (!res){
+         failed_tx ++;
+       }
+      }
+
+      sleep(1);
+      ping_xapp.Stop();
+      pong_xapp.Stop();
+
+      std::cerr <<"Pong received ping pkts = " << num_ping_pkts << " Ping received a1 packets = " << num_pong_pkts << " failures = " << failed_tx << std::endl;
       
-//       // send messages to ping
-//       Test_message my_message;
-//       uint32_t i = 0;
-//       for(i = 0; i < NumPkts; i++){
-//             clock_gettime(CLOCK_REALTIME, &(my_message.ts));
-//             snprintf(my_message.payload, MESSAGE_SIZE, "hello world %d", i);
-//             bool res = ping_xapp.Send(0, sizeof(Test_message), (void *) (&my_message));
-//             if (!res){
-//               failed_tx ++;
-//             }
-//       }
-
-//       sleep(1);
-//       ping_xapp.Stop();
-//       pong_xapp.Stop();
-
-//       REQUIRE(failed_tx == 0);
-//       REQUIRE(num_ping_pkts == NumPkts);
-//       REQUIRE(num_pong_pkts == NumPkts);
-
-//     }
-// }
-
-
-// TEST_CASE(" Test out various  transmission methods ..", "1"){
-
-//   // Test parameters
-//   char app_name[128] = "Test App";
-//   char port[16] = "tcp:4999";
+      REQUIRE(num_ping_pkts >=  NumPkts);
+      REQUIRE(num_pong_pkts >=  NumPkts);
+      REQUIRE(failed_tx == 0);
+
+
+      
+     
+    }
+}
+
+
+TEST_CASE(" Test out various  transmission methods ..", "1"){
+
+  // Test parameters
+  char app_name[128] = "Test App";
+  char port[16] = "tcp:4999";
   
+  init_logger("UNIT_TEST_XAPP", MDCLOG_INFO);
   
-//   mdclog_attr_t *attr;
-//   mdclog_attr_init(&attr);
-//   mdclog_attr_set_ident(attr, "UNIT TEST XAPP FRAMEWORK");
-//   mdclog_init(attr);
-//   mdclog_level_set(MDCLOG_INFO);
-//   mdclog_attr_destroy(attr);
-//   bool res;
-//   unsigned char my_meid[32] = "ABC123";
-//   Test_message my_message;
+  bool res;
+  unsigned char my_meid[32] = "ABC123";
+  Test_message my_message;
   
-//   SECTION("Test if message larger than allowed"){
-//     num_recv_pkts = 0;
-//     num_dropped_pkts = 0;
-//     failed_tx = 0;
+  SECTION("Test if message larger than allowed"){
+    num_recv_pkts = 0;
+    num_dropped_pkts = 0;
+    failed_tx = 0;
     
-//     // Instantiate and configure xAPP
-//     XaPP test_xapp = XaPP(app_name, port, sizeof(Test_message) , 1);
+    // Instantiate and configure xAPP
+    XaPP test_xapp = XaPP(app_name, port, sizeof(Test_message) );
     
-//     // Start receiver for test
-//     test_xapp.StartThread(&rcvd_pkts);
-//     sleep(1);
+    // Start receiver for test
+    test_xapp.StartThread(&rcvd_pkts);
+    sleep(1);
     
-//     // Test sending a message of size larger than allowed
-//     res = test_xapp.Send(0, RMR_BUFFER_SIZE + 100, (void *)(&my_message));
-//     test_xapp.Stop();
+    // Test sending a message of size larger than allowed
+    res = test_xapp.Send(102, RMR_BUFFER_SIZE + 100, (void *)(&my_message));
+    REQUIRE(res == false);
+
+    res = test_xapp.Send(102, RMR_BUFFER_SIZE + 100, (void *)(&my_message), "id");
+    REQUIRE(res == false);
+    test_xapp.Stop();
   
-//     REQUIRE(res == false);
-//   }
+
+  }
   
-//   SECTION("Test with tlv"){
-//     num_recv_pkts = 0;
-//     num_dropped_pkts = 0;
-//     failed_tx = 0;
+  SECTION("Test with tlv"){
+    num_recv_pkts = 0;
+    num_dropped_pkts = 0;
+    failed_tx = 0;
     
-//     // Instantiate and configure xAPP
-//     XaPP test_xapp = XaPP(app_name, port, sizeof(Test_message) , 1);
+    // Instantiate and configure xAPP
+    XaPP test_xapp = XaPP(app_name, port, sizeof(Test_message) );
 
-//     // Start receiver for test
-//     test_xapp.StartThread(&rcvd_pkts);
-//     sleep(1);
+    // Start receiver for test
+    test_xapp.StartThread(&rcvd_pkts);
+    sleep(1);
     
-//     // Test Send  with tlv
-//     clock_gettime(CLOCK_REALTIME, &(my_message.ts));
-//     snprintf(my_message.payload, MESSAGE_SIZE, "hello world");
-//     res = test_xapp.Send(0, sizeof(Test_message), (void *) (&my_message));
-//     sleep(1);
+    // Test Send  with tlv
+    clock_gettime(CLOCK_REALTIME, &(my_message.ts));
+    snprintf(my_message.payload, MESSAGE_SIZE, "hello world");
+    res = test_xapp.Send(102, sizeof(Test_message), (void *) (&my_message));
+    sleep(1);
     
-//     // Test send with tlv and meid
-//     res = test_xapp.Send(0, sizeof(Test_message), (void *) (&my_message), my_meid);
-//     sleep(1);
+    // Test send with tlv and meid
+    res = test_xapp.Send(102, sizeof(Test_message), (void *) (&my_message), my_meid);
+    sleep(1);
     
-//     test_xapp.Stop();
+    test_xapp.Stop();
     
-//     REQUIRE(num_recv_pkts == 2);
-//     REQUIRE(!strcmp((const char *)meid, (const char *)my_meid));
-//   }  
+    REQUIRE(num_recv_pkts == 2);
+    REQUIRE(!strcmp((const char *)meid, (const char *)my_meid));
+  }  
 }