Commit adb1be9a authored by dpranke@chromium.org's avatar dpranke@chromium.org

Remove several unneeded modules from webkitpy.common.

webkitpy.common.editdistance, webkitpy.common.lru_cache, and
webkitpy.common.thread were no longer being used.

webkitpy.common.newstringio could be (and should've been) replaced by
io.StringIO() or io.BytesIO() as appropriate to indicate if you were
dealing with text or bytes.

R=abarth@chromium.org

Review URL: https://codereview.chromium.org/479633002

git-svn-id: svn://svn.chromium.org/blink/trunk@180397 bbb929c8-8fbe-4397-9dbb-9b2b20218538
parent 00f3ce9a
...@@ -22,7 +22,6 @@ ...@@ -22,7 +22,6 @@
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# This file is used by: # This file is used by:
# webkitpy/tool/steps/addsvnmimetypeforpng.py
# webkitpy/style/checkers/png.py # webkitpy/style/checkers/png.py
import os import os
......
# Copyright (c) 2011 Google Inc. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
# * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from array import array
def edit_distance(str1, str2):
unsignedShort = 'h'
distances = [array(unsignedShort, (0,) * (len(str2) + 1)) for i in range(0, len(str1) + 1)]
# distances[0][0] = 0 since distance between str1[:0] and str2[:0] is 0
for i in range(1, len(str1) + 1):
distances[i][0] = i # Distance between str1[:i] and str2[:0] is i
for j in range(1, len(str2) + 1):
distances[0][j] = j # Distance between str1[:0] and str2[:j] is j
for i in range(0, len(str1)):
for j in range(0, len(str2)):
diff = 0 if str1[i] == str2[j] else 1
# Deletion, Insertion, Identical / Replacement
distances[i + 1][j + 1] = min(distances[i + 1][j] + 1, distances[i][j + 1] + 1, distances[i][j] + diff)
return distances[len(str1)][len(str2)]
# Copyright (c) 2010 Google Inc. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
# * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import unittest
from webkitpy.common.editdistance import edit_distance
class EditDistanceTest(unittest.TestCase):
def test_edit_distance(self):
self.assertEqual(edit_distance('', 'aa'), 2)
self.assertEqual(edit_distance('aa', ''), 2)
self.assertEqual(edit_distance('a', 'ab'), 1)
self.assertEqual(edit_distance('ab', 'a'), 1)
self.assertEqual(edit_distance('ab', 'aa'), 1)
self.assertEqual(edit_distance('aa', 'ab'), 1)
self.assertEqual(edit_distance('abd', 'abcdef'), 3)
self.assertEqual(edit_distance('abcdef', 'abd'), 3)
# Copyright (C) 2011 Google Inc. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
class Node():
def __init__(self, key, value):
self.key = key
self.value = value
self.prev = None
self.next = None
class LRUCache():
"""An implementation of Least Recently Used (LRU) Cache."""
def __init__(self, capacity):
"""Initializes a lru cache with the given capacity.
Args:
capacity: The capacity of the cache.
"""
assert capacity > 0, "capacity (%s) must be greater than zero." % capacity
self._first = None
self._last = None
self._dict = {}
self._capacity = capacity
def __setitem__(self, key, value):
if key in self._dict:
self.__delitem__(key)
if not self._first:
self._one_node(key, value)
return
if len(self._dict) >= self._capacity:
del self._dict[self._last.key]
if self._capacity == 1:
self._one_node(key, value)
return
self._last = self._last.next
self._last.prev = None
node = Node(key, value)
node.prev = self._first
self._first.next = node
self._first = node
self._dict[key] = node
def _one_node(self, key, value):
node = Node(key, value)
self._dict[key] = node
self._first = node
self._last = node
def __getitem__(self, key):
if not self._first:
raise KeyError(str(key))
if self._first.key == key:
return self._first.value
if self._last.key == key:
next_last = self._last.next
next_last.prev = None
next_first = self._last
next_first.prev = self._first
next_first.next = None
self._first.next = next_first
self._first = next_first
self._last = next_last
return self._first.value
node = self._dict[key]
node.next.prev = node.prev
node.prev.next = node.next
node.prev = self._first
node.next = None
self._first.next = node
self._first = node
return self._first.value
def __delitem__(self, key):
node = self._dict[key]
del self._dict[key]
if self._first is self._last:
self._last = None
self._first = None
return
if self._first is node:
self._first = node.prev
self._first.next = None
return
if self._last is node:
self._last = node.next
self._last.prev = None
return
node.next.prev = node.prev
node.prev.next = node.next
def __len__(self):
return len(self._dict)
def __contains__(self, key):
return key in self._dict
def __iter__(self):
return iter(self._dict)
def items(self):
return [(key, node.value) for key, node in self._dict.items()]
def values(self):
return [node.value for node in self._dict.values()]
def keys(self):
return self._dict.keys()
# Copyright (C) 2011 Google Inc. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import unittest
from webkitpy.common import lru_cache
class LRUCacheTest(unittest.TestCase):
def setUp(self):
self.lru = lru_cache.LRUCache(3)
self.lru['key_1'] = 'item_1'
self.lru['key_2'] = 'item_2'
self.lru['key_3'] = 'item_3'
self.lru2 = lru_cache.LRUCache(1)
self.lru2['key_1'] = 'item_1'
def test_items(self):
self.assertEqual(set(self.lru.items()), set([('key_1', 'item_1'), ('key_3', 'item_3'), ('key_2', 'item_2')]))
def test_put(self):
self.lru['key_4'] = 'item_4'
self.assertEqual(set(self.lru.items()), set([('key_4', 'item_4'), ('key_3', 'item_3'), ('key_2', 'item_2')]))
def test_update(self):
self.lru['key_1']
self.lru['key_5'] = 'item_5'
self.assertEqual(set(self.lru.items()), set([('key_1', 'item_1'), ('key_3', 'item_3'), ('key_5', 'item_5')]))
def test_keys(self):
self.assertEqual(set(self.lru.keys()), set(['key_1', 'key_2', 'key_3']))
def test_delete(self):
del self.lru['key_1']
self.assertFalse('key_1' in self.lru)
def test_contain(self):
self.assertTrue('key_1' in self.lru)
self.assertFalse('key_4' in self.lru)
def test_values(self):
self.assertEqual(set(self.lru.values()), set(['item_1', 'item_2', 'item_3']))
def test_len(self):
self.assertEqual(len(self.lru), 3)
def test_size_one_pop(self):
self.lru2['key_2'] = 'item_2'
self.assertEqual(self.lru2.keys(), ['key_2'])
def test_size_one_delete(self):
del self.lru2['key_1']
self.assertFalse('key_1' in self.lru2)
def test_pop_error(self):
self.assertRaises(KeyError, self.lru2.__getitem__, 'key_2')
del self.lru2['key_1']
self.assertRaises(KeyError, self.lru2.__getitem__, 'key_2')
def test_get_middle_item(self):
self.lru['key_2']
self.lru['key_4'] = 'item_4'
self.lru['key_5'] = 'item_5'
self.assertEqual(set(self.lru.keys()), set(['key_2', 'key_4', 'key_5']))
def test_set_again(self):
self.lru['key_1'] = 'item_4'
self.assertEqual(set(self.lru.items()), set([('key_1', 'item_4'), ('key_3', 'item_3'), ('key_2', 'item_2')]))
# Copyright (C) 2010 Google Inc. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
# * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""'with'-compliant StringIO implementation."""
import StringIO as OldStringIO
class StringIO(OldStringIO.StringIO):
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
pass
# Copyright (C) 2010 Google Inc. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
# * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""Unit tests for newstringio module."""
import unittest
import newstringio
class NewStringIOTest(unittest.TestCase):
def test_with(self):
with newstringio.StringIO("foo") as f:
contents = f.read()
self.assertEqual(contents, "foo")
# Required for Python to search this directory for module files
# Copyright (c) 2010 Google Inc. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
# * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
class MessagePumpDelegate(object):
def schedule(self, interval, callback):
raise NotImplementedError, "subclasses must implement"
def message_available(self, message):
raise NotImplementedError, "subclasses must implement"
def final_message_delivered(self):
raise NotImplementedError, "subclasses must implement"
class MessagePump(object):
interval = 10 # seconds
def __init__(self, delegate, message_queue):
self._delegate = delegate
self._message_queue = message_queue
self._schedule()
def _schedule(self):
self._delegate.schedule(self.interval, self._callback)
def _callback(self):
(messages, is_running) = self._message_queue.take_all()
for message in messages:
self._delegate.message_available(message)
if not is_running:
self._delegate.final_message_delivered()
return
self._schedule()
# Copyright (C) 2010 Google Inc. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
# * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import unittest
from webkitpy.common.thread.messagepump import MessagePump, MessagePumpDelegate
from webkitpy.common.thread.threadedmessagequeue import ThreadedMessageQueue
class TestDelegate(MessagePumpDelegate):
def __init__(self):
self.log = []
def schedule(self, interval, callback):
self.callback = callback
self.log.append("schedule")
def message_available(self, message):
self.log.append("message_available: %s" % message)
def final_message_delivered(self):
self.log.append("final_message_delivered")
class MessagePumpTest(unittest.TestCase):
def test_basic(self):
queue = ThreadedMessageQueue()
delegate = TestDelegate()
pump = MessagePump(delegate, queue)
self.assertEqual(delegate.log, [
'schedule'
])
delegate.callback()
queue.post("Hello")
queue.post("There")
delegate.callback()
self.assertEqual(delegate.log, [
'schedule',
'schedule',
'message_available: Hello',
'message_available: There',
'schedule'
])
queue.post("More")
queue.post("Messages")
queue.stop()
delegate.callback()
self.assertEqual(delegate.log, [
'schedule',
'schedule',
'message_available: Hello',
'message_available: There',
'schedule',
'message_available: More',
'message_available: Messages',
'final_message_delivered'
])
# Copyright (c) 2010 Google Inc. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
# * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import threading
class ThreadedMessageQueue(object):
def __init__(self):
self._messages = []
self._is_running = True
self._lock = threading.Lock()
def post(self, message):
with self._lock:
self._messages.append(message)
def stop(self):
with self._lock:
self._is_running = False
def take_all(self):
with self._lock:
messages = self._messages
is_running = self._is_running
self._messages = []
return (messages, is_running)
# Copyright (C) 2010 Google Inc. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
# * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import unittest
from webkitpy.common.thread.threadedmessagequeue import ThreadedMessageQueue
class ThreadedMessageQueueTest(unittest.TestCase):
def test_basic(self):
queue = ThreadedMessageQueue()
queue.post("Hello")
queue.post("There")
(messages, is_running) = queue.take_all()
self.assertEqual(messages, ["Hello", "There"])
self.assertTrue(is_running)
(messages, is_running) = queue.take_all()
self.assertEqual(messages, [])
self.assertTrue(is_running)
queue.post("More")
queue.stop()
queue.post("Messages")
(messages, is_running) = queue.take_all()
self.assertEqual(messages, ["More", "Messages"])
self.assertFalse(is_running)
(messages, is_running) = queue.take_all()
self.assertEqual(messages, [])
self.assertFalse(is_running)
...@@ -28,10 +28,10 @@ ...@@ -28,10 +28,10 @@
"""Unit tests for MockDRT.""" """Unit tests for MockDRT."""
import io
import sys import sys
import unittest import unittest
from webkitpy.common import newstringio
from webkitpy.common.system.systemhost_mock import MockSystemHost from webkitpy.common.system.systemhost_mock import MockSystemHost
from webkitpy.layout_tests.port import mock_drt from webkitpy.layout_tests.port import mock_drt
from webkitpy.layout_tests.port import port_testcase from webkitpy.layout_tests.port import port_testcase
...@@ -127,9 +127,9 @@ class MockDRTTest(unittest.TestCase): ...@@ -127,9 +127,9 @@ class MockDRTTest(unittest.TestCase):
pixel_tests, expected_checksum, drt_output, drt_input=None, expected_text=expected_text) pixel_tests, expected_checksum, drt_output, drt_input=None, expected_text=expected_text)
args = ['--dump-render-tree', '--platform', port_name, '-'] args = ['--dump-render-tree', '--platform', port_name, '-']
stdin = newstringio.StringIO(drt_input) stdin = io.BytesIO(drt_input)
stdout = newstringio.StringIO() stdout = io.BytesIO()
stderr = newstringio.StringIO() stderr = io.BytesIO()
options, args = mock_drt.parse_options(args) options, args = mock_drt.parse_options(args)
drt = self.make_drt(options, args, host, stdin, stdout, stderr) drt = self.make_drt(options, args, host, stdin, stdout, stderr)
...@@ -137,17 +137,15 @@ class MockDRTTest(unittest.TestCase): ...@@ -137,17 +137,15 @@ class MockDRTTest(unittest.TestCase):
self.assertEqual(res, 0) self.assertEqual(res, 0)
# We use the StringIO.buflist here instead of getvalue() because self.assertEqual(stdout.getvalue(), ''.join(drt_output))
# the StringIO might be a mix of unicode/ascii and 8-bit strings.
self.assertEqual(stdout.buflist, drt_output)
self.assertEqual(stderr.getvalue(), '#EOF\n') self.assertEqual(stderr.getvalue(), '#EOF\n')
def test_main(self): def test_main(self):
host = MockSystemHost() host = MockSystemHost()
test.add_unit_tests_to_mock_filesystem(host.filesystem) test.add_unit_tests_to_mock_filesystem(host.filesystem)
stdin = newstringio.StringIO() stdin = io.BytesIO()
stdout = newstringio.StringIO() stdout = io.BytesIO()
stderr = newstringio.StringIO() stderr = io.BytesIO()
res = mock_drt.main(['--dump-render-tree', '--platform', 'test', '-'], res = mock_drt.main(['--dump-render-tree', '--platform', 'test', '-'],
host, stdin, stdout, stderr) host, stdin, stdout, stderr)
self.assertEqual(res, 0) self.assertEqual(res, 0)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment