Selaa lähdekoodia

[Stress] Fixes JSON serialization of Big data.

Depends on celery/kombu@b94f1bab9b76177a525b7ef0fb44621031a607fa
Ask Solem 9 vuotta sitten
vanhempi
commit
d6fccea143
2 muutettua tiedostoa jossa 37 lisäystä ja 31 poistoa
  1. 2 1
      funtests/stress/stress/__init__.py
  2. 35 30
      funtests/stress/stress/data.py

+ 2 - 1
funtests/stress/stress/__init__.py

@@ -4,6 +4,8 @@ from __future__ import absolute_import
 import os
 import os
 import time
 import time
 
 
+from .data import install_json  # noqa
+
 if os.environ.get('C_SLEEP'):
 if os.environ.get('C_SLEEP'):
 
 
     _orig_sleep = time.sleep
     _orig_sleep = time.sleep
@@ -15,5 +17,4 @@ if os.environ.get('C_SLEEP'):
         _orig_sleep(n)
         _orig_sleep(n)
     time.sleep = _sleep
     time.sleep = _sleep
 
 
-
 from .app import app  # noqa
 from .app import app  # noqa

+ 35 - 30
funtests/stress/stress/data.py

@@ -1,14 +1,45 @@
 # -*- coding: utf-8 -*-
 # -*- coding: utf-8 -*-
 from __future__ import absolute_import
 from __future__ import absolute_import
 
 
-import json
-
-from celery.utils.debug import humanbytes
-from celery.utils.imports import qualname
+try:
+    import simplejson as json
+except ImportError:
+    import json  # noqa
 
 
 type_registry = {}
 type_registry = {}
 
 
 
 
+class JSONEncoder(json.JSONEncoder):
+
+    def default(self, obj):
+        try:
+            return super(JSONEncoder, self).default(obj)
+        except TypeError:
+            reducer = getattr(obj, '__to_json__', None)
+            if reducer:
+                return reducer()
+            raise
+
+
+def decode_hook(d):
+    try:
+        d = d['py/obj']
+    except KeyError:
+        return d
+    type_registry[d['type']](**d['attrs'])
+
+
+def install_json():
+    json._default_encoder = JSONEncoder()
+    json._default_decoder.object_hook = decode_hook
+install_json()  # ugh, ugly but it's a test suite after all
+
+
+# this imports kombu.utils.json, so can only import after install_json()
+from celery.utils.debug import humanbytes  # noqa
+from celery.utils.imports import qualname  # noqa
+
+
 def json_reduce(obj, attrs):
 def json_reduce(obj, attrs):
     return {'py/obj': {'type': qualname(obj), 'attrs': attrs}}
     return {'py/obj': {'type': qualname(obj), 'attrs': attrs}}
 
 
@@ -43,29 +74,3 @@ class Data(object):
 
 
 BIG = Data('BIG', 'x' * 2 ** 20 * 8)
 BIG = Data('BIG', 'x' * 2 ** 20 * 8)
 SMALL = Data('SMALL', 'e' * 1024)
 SMALL = Data('SMALL', 'e' * 1024)
-
-
-class JSONEncoder(json.JSONEncoder):
-
-    def default(self, obj):
-        try:
-            return super(JSONEncoder, self).default(obj)
-        except TypeError:
-            reducer = getattr(obj, '__to_json__', None)
-            if reducer:
-                return reducer()
-            raise
-
-
-def decode_hook(d):
-    try:
-        d = d['py/obj']
-    except KeyError:
-        return d
-    type_registry[d['type']](**d['attrs'])
-
-
-def install_json():
-    json._default_encoder = JSONEncoder()
-    json._default_decoder.object_hook = decode_hook
-install_json()  # ugh, ugly but it's a test suite after all