vpp_papi: Context_id allocator for running forked.
[vpp.git] / src / vpp-api / python / vpp_papi / tests / test_vpp_papi.py
1 #  Copyright (c) 2019. Vinci Consulting Corp. All Rights Reserved.
2 #
3 #  Licensed under the Apache License, Version 2.0 (the "License");
4 #  you may not use this file except in compliance with the License.
5 #  You may obtain a copy of the License at
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
9 #  Unless required by applicable law or agreed to in writing, software
10 #  distributed under the License is distributed on an "AS IS" BASIS,
11 #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 #  See the License for the specific language governing permissions and
13 #  limitations under the License.
14
15 import ctypes
16 import multiprocessing as mp
17 import unittest
18 from vpp_papi import vpp_papi
19
20
21 class TestVppPapiVPPApiClient(unittest.TestCase):
22
23     def test_getcontext(self):
24         vpp_papi.VPPApiClient.apidir = '.'
25         c = vpp_papi.VPPApiClient(testmode=True, use_socket=True)
26
27         # reset initialization at module load time.
28         c.get_context.context = mp.Value(ctypes.c_uint, 0)
29         for _ in range(10):
30             c.get_context()
31         self.assertEqual(11, c.get_context())
32
33
34 class TestVppPapiVPPApiClientMp(unittest.TestCase):
35     # Test under multiple processes to simulate running forked under
36     # run_tests.py (eg. make test TEST_JOBS=10)
37
38     def test_get_context_mp(self):
39         vpp_papi.VPPApiClient.apidir = '.'
40         c = vpp_papi.VPPApiClient(testmode=True, use_socket=True)
41
42         # reset initialization at module load time.
43         c.get_context.context = mp.Value(ctypes.c_uint, 0)
44         procs = [mp.Process(target=c.get_context, args=()) for i in range(10)]
45
46         for p in procs:
47             p.start()
48         for p in procs:
49             p.join()
50
51         # AssertionError: 11 != 1
52         self.assertEqual(11, c.get_context())
53