Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame^] | 1 | # ex:ts=4:sw=4:sts=4:et |
| 2 | # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- |
| 3 | # |
| 4 | # BitBake Test for lib/bb/persist_data/ |
| 5 | # |
| 6 | # Copyright (C) 2018 Garmin Ltd. |
| 7 | # |
| 8 | # This program is free software; you can redistribute it and/or modify |
| 9 | # it under the terms of the GNU General Public License version 2 as |
| 10 | # published by the Free Software Foundation. |
| 11 | # |
| 12 | # This program is distributed in the hope that it will be useful, |
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | # GNU General Public License for more details. |
| 16 | # |
| 17 | # You should have received a copy of the GNU General Public License along |
| 18 | # with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | # |
| 21 | |
| 22 | import unittest |
| 23 | import bb.data |
| 24 | import bb.persist_data |
| 25 | import tempfile |
| 26 | import threading |
| 27 | |
| 28 | class PersistDataTest(unittest.TestCase): |
| 29 | def _create_data(self): |
| 30 | return bb.persist_data.persist('TEST_PERSIST_DATA', self.d) |
| 31 | |
| 32 | def setUp(self): |
| 33 | self.d = bb.data.init() |
| 34 | self.tempdir = tempfile.TemporaryDirectory() |
| 35 | self.d['PERSISTENT_DIR'] = self.tempdir.name |
| 36 | self.data = self._create_data() |
| 37 | self.items = { |
| 38 | 'A1': '1', |
| 39 | 'B1': '2', |
| 40 | 'C2': '3' |
| 41 | } |
| 42 | self.stress_count = 10000 |
| 43 | self.thread_count = 5 |
| 44 | |
| 45 | for k,v in self.items.items(): |
| 46 | self.data[k] = v |
| 47 | |
| 48 | def tearDown(self): |
| 49 | self.tempdir.cleanup() |
| 50 | |
| 51 | def _iter_helper(self, seen, iterator): |
| 52 | with iter(iterator): |
| 53 | for v in iterator: |
| 54 | self.assertTrue(v in seen) |
| 55 | seen.remove(v) |
| 56 | self.assertEqual(len(seen), 0, '%s not seen' % seen) |
| 57 | |
| 58 | def test_get(self): |
| 59 | for k, v in self.items.items(): |
| 60 | self.assertEqual(self.data[k], v) |
| 61 | |
| 62 | self.assertIsNone(self.data.get('D')) |
| 63 | with self.assertRaises(KeyError): |
| 64 | self.data['D'] |
| 65 | |
| 66 | def test_set(self): |
| 67 | for k, v in self.items.items(): |
| 68 | self.data[k] += '-foo' |
| 69 | |
| 70 | for k, v in self.items.items(): |
| 71 | self.assertEqual(self.data[k], v + '-foo') |
| 72 | |
| 73 | def test_delete(self): |
| 74 | self.data['D'] = '4' |
| 75 | self.assertEqual(self.data['D'], '4') |
| 76 | del self.data['D'] |
| 77 | self.assertIsNone(self.data.get('D')) |
| 78 | with self.assertRaises(KeyError): |
| 79 | self.data['D'] |
| 80 | |
| 81 | def test_contains(self): |
| 82 | for k in self.items: |
| 83 | self.assertTrue(k in self.data) |
| 84 | self.assertTrue(self.data.has_key(k)) |
| 85 | self.assertFalse('NotFound' in self.data) |
| 86 | self.assertFalse(self.data.has_key('NotFound')) |
| 87 | |
| 88 | def test_len(self): |
| 89 | self.assertEqual(len(self.data), len(self.items)) |
| 90 | |
| 91 | def test_iter(self): |
| 92 | self._iter_helper(set(self.items.keys()), self.data) |
| 93 | |
| 94 | def test_itervalues(self): |
| 95 | self._iter_helper(set(self.items.values()), self.data.itervalues()) |
| 96 | |
| 97 | def test_iteritems(self): |
| 98 | self._iter_helper(set(self.items.items()), self.data.iteritems()) |
| 99 | |
| 100 | def test_get_by_pattern(self): |
| 101 | self._iter_helper({'1', '2'}, self.data.get_by_pattern('_1')) |
| 102 | |
| 103 | def _stress_read(self, data): |
| 104 | for i in range(self.stress_count): |
| 105 | for k in self.items: |
| 106 | data[k] |
| 107 | |
| 108 | def _stress_write(self, data): |
| 109 | for i in range(self.stress_count): |
| 110 | for k, v in self.items.items(): |
| 111 | data[k] = v + str(i) |
| 112 | |
| 113 | def _validate_stress(self): |
| 114 | for k, v in self.items.items(): |
| 115 | self.assertEqual(self.data[k], v + str(self.stress_count - 1)) |
| 116 | |
| 117 | def test_stress(self): |
| 118 | self._stress_read(self.data) |
| 119 | self._stress_write(self.data) |
| 120 | self._validate_stress() |
| 121 | |
| 122 | def test_stress_threads(self): |
| 123 | def read_thread(): |
| 124 | data = self._create_data() |
| 125 | self._stress_read(data) |
| 126 | |
| 127 | def write_thread(): |
| 128 | data = self._create_data() |
| 129 | self._stress_write(data) |
| 130 | |
| 131 | threads = [] |
| 132 | for i in range(self.thread_count): |
| 133 | threads.append(threading.Thread(target=read_thread)) |
| 134 | threads.append(threading.Thread(target=write_thread)) |
| 135 | |
| 136 | for t in threads: |
| 137 | t.start() |
| 138 | self._stress_read(self.data) |
| 139 | for t in threads: |
| 140 | t.join() |
| 141 | self._validate_stress() |
| 142 | |