blob: 4965b1e8dfd90c068cf3932e232ac294bf5a85da [file] [log] [blame]
E. Scott Daniels2301eb82020-07-15 13:46:51 -04001// vi: ts=4 sw=4 noet:
2/*
3==================================================================================
4 Copyright (c) 2020 AT&T Intellectual Property.
5 Copyright (c) 2020 Nokia
6
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11 http://www.apache.org/licenses/LICENSE-2.0
12
13 Unless required by applicable law or agreed to in writing, software
14 distributed under the License is distributed on an "AS IS" BASIS,
15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 See the License for the specific language governing permissions and
17 limitations under the License.
18==================================================================================
19*/
20
21/*
22 Mnemonic: context_test.cpp
23 Abstract: Unit test for the context code
24
25 Date: 19 June 2020
26 Author: E. Scott Daniels
27*/
28
29#include "ricxfcpp/message.hpp" // must have for callback funciton
30#include "ricxfcpp/xapp.hpp"
31
32#include "ves_msg.h"
33#include "rthing.h"
34
35#include "context.h" // things under test are included directly
36#include "context.cpp"
37
38#include "ut_support.cpp"
39
40/*
41 Function to pass to register callback; does nothing pratical in the test.
42*/
E. Scott Daniels10ca4f92020-07-23 15:45:35 -040043extern void ctx_test_cb( xapp::Message& mbuf, int mtype, int subid, int len, xapp::Msg_component payload, void* data ) {
E. Scott Daniels2301eb82020-07-15 13:46:51 -040044 fprintf( stderr, "<INFO> test_cb: should never see this message\n" );
45}
46
47int main( int argc, char** argv ) {
48 munchkin::Context ctx1( "4560", false ); // create without waiting for rmr ready
49 munchkin::Context ctx2( "5560", false );
50 munchkin::Context* ctxp;
51 std::shared_ptr<munchkin::Ves_sender> sender;
52 std::shared_ptr<munchkin::Rthing> rt;
53 std::shared_ptr<munchkin::Rthing> rt2;
54 std::string ves_target = "http://localhost:21989";
55 int errors = 0;
56 int count; // count set in the object
57 int socount; // shared object use count
58
59 set_test_name( "context_test" );
60
61 ctx2 = ctx1; // copy operator
62 munchkin::Context ctx3 = ctx1; // copy construction
63
64 ctx2 = std::move( ctx3 ); // move assignment
65 munchkin::Context ctx4 = std::move( ctx1 ); // move constructor
66
67 rt = ctx2.Find_rt( "buttmunch:43086" ); // non-existant rt, should create
68 if( fail_if( rt == NULL, "find rt didn't return a pointer" ) != 0 ) {
69 announce_results( errors + 1 );
70 return 1;
71 }
72
73 socount = rt.use_count(); // get current use
74 rt->Inc_count();
75 count = rt->Get_count();
76 errors += fail_if( count != 1, "inc rt count failed" );
77
78 rt2 = ctx2.Find_rt( "buttmunch:43086" ); // existing rt, should return pointer to same rt
79 if( fail_if( rt2 == NULL, "find rt didn't return a pointer to existing rt" ) != 0 ) {
80 announce_results( errors + 1 );
81 return 1;
82 }
83 errors += fail_if( socount == rt.use_count(), "rt2 appears to reference the wrong object" );
84
85 count = rt2->Get_count();
86 errors += fail_if( count != 1, "rt2 count didnt match expected count" );
87
88 rt->Inc_count( 5 );
89 count = rt2->Get_count();
90 errors += fail_if( count != 6, "get of rt2 count didnt match expected 6 set with rt" );
91
92
93 ctx1.Register_cb( 203, ctx_test_cb ); // drive for coverage; nothing to check
94 ctx1.Register_cb( -1, ctx_test_cb ); // register as default
95
96 ctxp = new munchkin::Context( "43086", false, ves_target ); // drive alternate for coverage
97 errors += fail_if( ctxp == NULL, "unable to create a context with ves url" );
98
99 sender = ctxp->Get_sender();
100 errors += fail_if( sender == NULL, "no sender pointer returned" );
101
102 announce_results( errors );
103 return !!errors;
104}
105