File: parallel_test.py

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (155 lines) | stat: -rwxr-xr-x 5,492 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env python3
# Copyright 2017 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import os
import threading
import unittest

import parallel


def _ForkTestHelper(arg1, arg2, pickle_me_not, test_instance, parent_pid):
  _ = pickle_me_not  # Suppress lint warning.
  test_instance.assertNotEqual(os.getpid(), parent_pid)
  return arg1 + arg2


class Unpicklable:
  """Ensures that pickle() is not called on parameters."""

  def __getstate__(self):
    raise AssertionError('Tried to pickle')


class ConcurrentTest(unittest.TestCase):
  def testEncodeDictOfLists_Empty(self):
    test_dict = {}
    encoded = parallel.EncodeDictOfLists(test_dict)
    decoded = parallel.DecodeDictOfLists(encoded)
    self.assertEqual(test_dict, decoded)

  def testEncodeDictOfLists_EmptyValue(self):
    test_dict = {'foo': []}
    encoded = parallel.EncodeDictOfLists(test_dict)
    decoded = parallel.DecodeDictOfLists(encoded)
    self.assertEqual(test_dict, decoded)

  def testEncodeDictOfLists_AllStrings(self):
    test_dict = {'foo': ['a', 'b', 'c'], 'foo2': ['a', 'b']}
    encoded = parallel.EncodeDictOfLists(test_dict)
    decoded = parallel.DecodeDictOfLists(encoded)
    self.assertEqual(test_dict, decoded)

  def testEncodeDictOfLists_KeyTransform(self):
    test_dict = {0: ['a', 'b', 'c'], 9: ['a', 'b']}
    encoded = parallel.EncodeDictOfLists(test_dict, key_transform=str)
    decoded = parallel.DecodeDictOfLists(encoded, key_transform=int)
    self.assertEqual(test_dict, decoded)

  def testEncodeDictOfLists_ValueTransform(self):
    test_dict = {'a': ['0', '1', '2'], 'b': ['3', '4']}
    expected = {'a': [0, 1, 2], 'b': [3, 4]}
    encoded = parallel.EncodeDictOfLists(test_dict)
    decoded = parallel.DecodeDictOfLists(encoded, value_transform=int)
    self.assertEqual(expected, decoded)

  def testEncodeDictOfLists_Join_Empty(self):
    test_dict1 = {}
    test_dict2 = {}
    expected = {}
    encoded1 = parallel.EncodeDictOfLists(test_dict1)
    encoded2 = parallel.EncodeDictOfLists(test_dict2)
    encoded = parallel.JoinEncodedDictOfLists([encoded1, encoded2])
    decoded = parallel.DecodeDictOfLists(encoded)
    self.assertEqual(expected, decoded)

  def testEncodeDictOfLists_Join_Singl(self):
    test_dict1 = {'key1': ['a']}
    encoded1 = parallel.EncodeDictOfLists(test_dict1)
    encoded = parallel.JoinEncodedDictOfLists([encoded1])
    decoded = parallel.DecodeDictOfLists(encoded)
    self.assertEqual(test_dict1, decoded)

  def testEncodeDictOfLists_JoinMultiple(self):
    test_dict1 = {'key1': ['a']}
    test_dict2 = {'key2': ['b']}
    expected = {'key1': ['a'], 'key2': ['b']}
    encoded1 = parallel.EncodeDictOfLists(test_dict1)
    encoded2 = parallel.EncodeDictOfLists({})
    encoded3 = parallel.EncodeDictOfLists(test_dict2)
    encoded = parallel.JoinEncodedDictOfLists([encoded1, encoded2, encoded3])
    decoded = parallel.DecodeDictOfLists(encoded)
    self.assertEqual(expected, decoded)

  def testCallOnThread(self):
    main_thread = threading.current_thread()

    def callback(arg1, arg2):
      self.assertEqual(1, arg1)
      self.assertEqual(2, arg2)
      my_thread = threading.current_thread()
      self.assertNotEqual(my_thread, main_thread)
      return 3

    result = parallel.CallOnThread(callback, 1, arg2=2)
    self.assertEqual(3, result.get())

  def testForkAndCall_normal(self):
    parent_pid = os.getpid()
    result = parallel.ForkAndCall(_ForkTestHelper,
                                  (1, 2, Unpicklable(), self, parent_pid))
    self.assertEqual(3, result.get())

  def testForkAndCall_exception(self):
    parent_pid = os.getpid()
    result = parallel.ForkAndCall(_ForkTestHelper,
                                  (1, 'a', None, self, parent_pid))
    self.assertRaises(TypeError, result.get)

  def testBulkForkAndCall_none(self):
    results = parallel.BulkForkAndCall(_ForkTestHelper, [])
    self.assertEqual([], list(results))

  def testBulkForkAndCall_few(self):
    parent_pid = os.getpid()
    results = parallel.BulkForkAndCall(_ForkTestHelper,
                                       [(1, 2, Unpicklable(), self, parent_pid),
                                        (3, 4, None, self, parent_pid)])
    self.assertEqual({3, 7}, set(results))

  def testBulkForkAndCall_few_kwargs(self):
    parent_pid = os.getpid()
    results = parallel.BulkForkAndCall(
        _ForkTestHelper, [(1, 2, Unpicklable()), (3, 4, None)],
        test_instance=self,
        parent_pid=parent_pid)
    self.assertEqual({3, 7}, set(results))

  def testBulkForkAndCall_many(self):
    parent_pid = os.getpid()
    args = [(1, 2, Unpicklable(), self, parent_pid) for _ in range(100)]
    results = parallel.BulkForkAndCall(_ForkTestHelper, args)
    self.assertEqual([3] * 100, list(results))

  def testBulkForkAndCall_many_kwargs(self):
    parent_pid = os.getpid()
    args = [(1, 2) for _ in range(100)]
    results = parallel.BulkForkAndCall(
        _ForkTestHelper,
        args,
        pickle_me_not=Unpicklable(),
        test_instance=self,
        parent_pid=parent_pid)
    self.assertEqual([3] * 100, list(results))

  def testBulkForkAndCall_exception(self):
    parent_pid = os.getpid()
    results = parallel.BulkForkAndCall(_ForkTestHelper,
                                       [(1, 'a', None, self, parent_pid)])
    self.assertRaises(TypeError, results.__next__)


if __name__ == '__main__':
  unittest.main()