예제 #1
0
 def setUp(self):
     self.serializer = Serializer()
예제 #2
0
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

import zmq

import droplet.server.utils as sutils
from droplet.shared.serializer import Serializer

serializer = Serializer()


class AbstractDropletUserLibrary:
    # Stores a lattice value at ref.
    def put(self, ref, ltc):
        raise NotImplementedError

    # Retrives the lattice value at ref.
    def get(self, ref, ltype):
        raise NotImplementedError

    # Sends a bytestring message to the specified destination.
    # TODO: type and format for destination ID?
    def send(self, dest, bytestr):
        raise NotImplementedError
예제 #3
0
class TestSerializer(unittest.TestCase):
    '''
    This test suite tests various serializer interface methods to ensure that
    they serialize data correctly and raise errors on unknown types.
    '''
    def setUp(self):
        self.serializer = Serializer()

    def test_serialize_obj(self):
        '''
        Tests that a normal Python object is serialized correctly.
        '''
        obj = {'a set'}

        serialized = self.serializer.dump(obj, serialize=False)

        self.assertEqual(type(serialized), Value)
        self.assertEqual(serialized.type, DEFAULT)

        self.assertEqual(self.serializer.load(serialized), obj)

    def test_serialize_numpy(self):
        '''
        Tests that a numpy array is correctly serialized with PyArrow.
        '''
        obj = np.random.randn(100, 100)

        serialized = self.serializer.dump(obj, serialize=False)

        self.assertEqual(type(serialized), Value)
        self.assertEqual(serialized.type, NUMPY)

        deserialized = self.serializer.load(serialized)
        self.assertTrue(np.array_equal(deserialized, obj))

    def test_serialize_to_bytes(self):
        '''
        Tests that the serializer correctly converts to a serialized protobuf.
        '''
        obj = {'a set'}

        val = Value()
        serialized = self.serializer.dump(obj, val, True)

        self.assertEqual(type(serialized), bytes)
        val.ParseFromString(serialized)
        self.assertEqual(val.type, DEFAULT)

        self.assertEqual(self.serializer.load(serialized), obj)

    def test_serialize_future(self):
        '''
        Tests that the serializer correctly detects and converts a
        DropletFuture to a DropletReference.
        '''

        kvs_client = MockAnnaClient()
        future = DropletFuture('id', kvs_client, self.serializer)

        serialized = self.serializer.dump(future, serialize=False)

        self.assertEqual(type(serialized), Value)
        self.assertEqual(serialized.type, DEFAULT)

        reference = self.serializer.load(serialized)
        self.assertEqual(type(reference), DropletReference)
        self.assertEqual(future.obj_id, reference.key)

    def test_serializer_map_lattice(self):
        '''
        Tests that the serializer correctly converts to and from a map lattice.
        '''

        inpt = {'a': 1, 'b': 2}
        serialized = self.serializer.dump_lattice(inpt)

        self.assertEqual(type(serialized), MapLattice)
        output = self.serializer.load_lattice(serialized)

        self.assertEqual(output, inpt)