Changes to framework usage:
[ric-plt/xapp-frame-py.git] / tests / test_xapps.py
1 # ==================================================================================
2 #       Copyright (c) 2020 Nokia
3 #       Copyright (c) 2020 AT&T Intellectual Property.
4 #
5 #   Licensed under the Apache License, Version 2.0 (the "License");
6 #   you may not use this file except in compliance with the License.
7 #   You may obtain a copy of the License at
8 #
9 #          http://www.apache.org/licenses/LICENSE-2.0
10 #
11 #   Unless required by applicable law or agreed to in writing, software
12 #   distributed under the License is distributed on an "AS IS" BASIS,
13 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 #   See the License for the specific language governing permissions and
15 #   limitations under the License.
16 # ==================================================================================
17 import json
18 import time
19 from contextlib import suppress
20 from ricxappframe.xapp_frame import Xapp, RMRXapp
21
22 rmr_xapp = None
23 gen_xapp = None
24
25
26 def test_flow():
27
28     # test variables
29     def_called = 0
30     sixty_called = 0
31
32     # create rmr app
33     global rmr_xapp
34
35     def post_init(self):
36         print("hey")
37
38     def default_handler(self, summary, sbuf):
39         nonlocal def_called
40         def_called += 1
41         assert json.loads(summary["payload"]) == {"test send 60001": 1}
42         self.rmr_free(sbuf)
43
44     rmr_xapp = RMRXapp(default_handler, post_init=post_init, rmr_port=4564, rmr_wait_for_ready=False, use_fake_sdl=True)
45
46     def sixtythou_handler(self, summary, sbuf):
47         nonlocal sixty_called
48         sixty_called += 1
49         assert json.loads(summary["payload"]) == {"test send 60000": 1}
50         self.rmr_free(sbuf)
51
52     rmr_xapp.register_callback(sixtythou_handler, 60000)
53     rmr_xapp.run()
54
55     time.sleep(1)
56
57     def entry(self):
58
59         self.sdl_set("testns", "mykey", 6)
60         assert self.sdl_get("testns", "mykey") == 6
61         assert self.sdl_find_and_get("testns", "myk") == {"mykey": 6}
62         assert self.healthcheck()
63
64         val = json.dumps({"test send 60000": 1}).encode()
65         self.rmr_send(val, 60000)
66
67         val = json.dumps({"test send 60001": 2}).encode()
68         self.rmr_send(val, 60001)
69
70     global gen_xapp
71     gen_xapp = Xapp(entrypoint=entry, rmr_wait_for_ready=False, use_fake_sdl=True)
72     gen_xapp.run()
73
74     time.sleep(1)
75
76     assert def_called == 1
77     assert sixty_called == 1
78
79
80 def teardown_module():
81     """
82     this is like a "finally"; the name of this function is pytest magic
83     safer to put down here since certain failures above can lead to pytest never returning
84     for example if an exception gets raised before stop is called in any test function above, pytest will hang forever
85     """
86     with suppress(Exception):
87         gen_xapp.stop()
88     with suppress(Exception):
89         rmr_xapp.stop()