Ejemplo n.º 1
0
  def testDeleteTooManyEntities(self):
    """Test putting more than allowed entity count."""
    max_entity_count = context.MAX_ENTITY_COUNT
    self.pool = context.MutationPool() # default size is MAX_ENTITY_COUNT

    m = mox.Mox()
    m.StubOutWithMock(datastore, 'Delete', use_mock_anything=True)

    entities = []
    for i in range(max_entity_count + 50):
      entities.append(TestEntity(key_name='die%d' % i))

    # Record Calls
    self.record_delete([e.key() for e in entities[:max_entity_count]])

    m.ReplayAll()
    try:  # test, verify
      for e in entities:
        self.pool.delete(e)

      # only 50 entities should be left.
      self.assertEquals(50, self.pool.deletes.length)

      m.VerifyAll()
    finally:
      m.UnsetStubs()
Ejemplo n.º 2
0
  def testFlush(self):
    """Test flush method."""
    self.pool = context.MutationPool(1000)

    m = mox.Mox()
    m.StubOutWithMock(datastore, 'Delete', use_mock_anything=True)
    m.StubOutWithMock(datastore, 'Put', use_mock_anything=True)

    e1 = TestEntity()
    e2 = TestEntity(key_name='flushme')

    # Record Calls
    self.record_put([e1._populate_internal_entity()])
    self.record_delete([e2.key()])

    m.ReplayAll()
    try:  # test, verify
      self.pool.put(e1)
      self.assertEquals([e1._populate_internal_entity()], self.pool.puts.items)

      self.pool.delete(e2)
      self.assertEquals([e2.key()], self.pool.deletes.items)

      self.pool.flush()

      self.assertEquals([], self.pool.puts.items)
      self.assertEquals([], self.pool.deletes.items)

      m.VerifyAll()
    finally:
      m.UnsetStubs()
Ejemplo n.º 3
0
  def testPutTooManyEntities(self):
    """Test putting more than allowed entity count."""
    max_entity_count = 100
    self.pool = context.MutationPool(max_entity_count=max_entity_count)

    m = mox.Mox()
    m.StubOutWithMock(datastore, 'Put', use_mock_anything=True)

    entities = []
    for i in range(max_entity_count + 50):
      entities.append(TestEntity())

    # Record Calls
    self.record_put([e._populate_internal_entity()
                     for e in entities[:max_entity_count]])

    m.ReplayAll()
    try:  # test, verify
      for e in entities:
        self.pool.put(e)

      # only 50 entities should be left.
      self.assertEquals(50, self.pool.puts.length)

      m.VerifyAll()
    finally:
      m.UnsetStubs()
Ejemplo n.º 4
0
  def setUp(self):
    unittest.TestCase.setUp(self)
    self.mox = mox.Mox()

    self.appid = "testapp"
    self.version_id = "1.23456789"
    os.environ["APPLICATION_ID"] = self.appid
    os.environ["CURRENT_VERSION_ID"] = self.version_id
    os.environ["HTTP_HOST"] = "localhost"

    self.memcache = memcache_stub.MemcacheServiceStub()
    self.taskqueue = taskqueue_stub.TaskQueueServiceStub()
    self.taskqueue.queue_yaml_parser = (
        lambda x: queueinfo.LoadSingleQueue(
            "queue:\n"
            "- name: default\n"
            "  rate: 10/s\n"
            "- name: crazy-queue\n"
            "  rate: 2000/d\n"
            "  bucket_size: 10\n"))
    self.datastore = datastore_file_stub.DatastoreFileStub(
        self.appid, "/dev/null", "/dev/null")

    self.blob_storage_directory = tempfile.mkdtemp()
    blob_storage = file_blob_storage.FileBlobStorage(
        self.blob_storage_directory, self.appid)
    self.blobstore_stub = blobstore_stub.BlobstoreServiceStub(blob_storage)
    self.file_service = self.createFileServiceStub(blob_storage)

    apiproxy_stub_map.apiproxy = apiproxy_stub_map.APIProxyStubMap()
    apiproxy_stub_map.apiproxy.RegisterStub("taskqueue", self.taskqueue)
    apiproxy_stub_map.apiproxy.RegisterStub("memcache", self.memcache)
    apiproxy_stub_map.apiproxy.RegisterStub("datastore_v3", self.datastore)
    apiproxy_stub_map.apiproxy.RegisterStub("blobstore", self.blobstore_stub)
    apiproxy_stub_map.apiproxy.RegisterStub("file", self.file_service)
 def testInitJob_GoogleStorage(self):
   mapreduce_state = self.create_mapreduce_state(
       params={"filesystem": "gs", "gs_bucket_name": "foo", "gs_acl": "public"})
   m = mox.Mox()
   m.StubOutWithMock(files.gs, "create")
   files.gs.create(mox.StrContains('/gs/foo'),
                   mox.IgnoreArg(),
                   acl="public")
   m.ReplayAll()
   output_writers.FileOutputWriter.init_job(mapreduce_state)
   m.UnsetStubs()
   m.VerifyAll()
   self.assertTrue(mapreduce_state.writer_state)
Ejemplo n.º 6
0
    def testIncrement(self):
        """Test applying Increment operation."""
        m = mox.Mox()

        ctx = context.Context(None, None)
        ctx.counters = m.CreateMock(context.Counters)

        operation = op.counters.Increment("test", 12)

        # Record calls
        ctx.counters.increment("test", 12)

        m.ReplayAll()
        try:  # test, verify
            operation(ctx)
            m.VerifyAll()
        finally:
            m.UnsetStubs()
Ejemplo n.º 7
0
    def testArbitraryPool(self):
        """Test arbitrary pool registration."""
        m = mox.Mox()

        ctx = context.Context(None, None)
        self.assertFalse(ctx.get_pool("test"))
        pool = m.CreateMockAnything()
        ctx.register_pool("test", pool)
        self.assertEquals(pool, ctx.get_pool("test"))

        # Record calls
        pool.flush()

        m.ReplayAll()
        try:  # test, verify
            ctx.flush()
            m.VerifyAll()
        finally:
            m.UnsetStubs()
Ejemplo n.º 8
0
  def testDelete(self):
    """Test applying Delete operation."""
    m = mox.Mox()

    ctx = context.Context(None, None)
    ctx.mutation_pool = m.CreateMock(context.MutationPool)

    entity = TestEntity()
    operation = op.db.Delete(entity)

    # Record calls
    ctx.mutation_pool.delete(entity)

    m.ReplayAll()
    try:  # test, verify
      operation(ctx)
      m.VerifyAll()
    finally:
      m.UnsetStubs()
Ejemplo n.º 9
0
    def setUp(self):
        unittest.TestCase.setUp(self)
        self.mox = mox.Mox()

        self.appid = "testapp"
        self.major_version_id = "1"
        self.version_id = self.major_version_id + ".23456789"
        self.module_id = "foo_module"
        self.host = "%s.%s.%s" % (self.major_version_id, self.module_id,
                                  "testapp.appspot.com")

        self.testbed = testbed.Testbed()
        self.testbed.activate()

        os.environ["APPLICATION_ID"] = self.appid
        os.environ["CURRENT_VERSION_ID"] = self.version_id
        os.environ["CURRENT_MODULE_ID"] = self.module_id
        os.environ["DEFAULT_VERSION_HOSTNAME"] = "%s.appspot.com" % self.appid
        os.environ["HTTP_HOST"] = self.host

        self.testbed.init_app_identity_stub()
        self.testbed.init_blobstore_stub()
        # HRD with no eventual consistency.
        policy = datastore_stub_util.PseudoRandomHRConsistencyPolicy(
            probability=1)
        self.testbed.init_datastore_v3_stub(consistency_policy=policy)
        self.testbed.init_logservice_stub()
        self.testbed.init_files_stub()
        self.testbed.init_memcache_stub()
        self.testbed.init_taskqueue_stub()
        self.testbed.init_urlfetch_stub()

        # For backwards compatibility, maintain easy references to some stubs
        self.taskqueue = self.testbed.get_stub(testbed.TASKQUEUE_SERVICE_NAME)

        self.taskqueue.queue_yaml_parser = (
            lambda x: queueinfo.LoadSingleQueue("queue:\n"
                                                "- name: default\n"
                                                "  rate: 10/s\n"
                                                "- name: crazy-queue\n"
                                                "  rate: 2000/d\n"
                                                "  bucket_size: 10\n"))
Ejemplo n.º 10
0
    def testIncrement(self):
        """Test increment() method."""
        m = mox.Mox()

        # Set up mocks
        shard_state = m.CreateMockAnything()
        counters_map = m.CreateMockAnything()
        shard_state.counters_map = counters_map
        counters = context._Counters(shard_state)

        # Record call
        counters_map.increment('test', 19)

        m.ReplayAll()
        try:  # test, verify
            counters.increment('test', 19)

            m.VerifyAll()
        finally:
            m.UnsetStubs()
Ejemplo n.º 11
0
 def testPoolWithForceWrites(self):
   class MapreduceSpec:
     def __init__(self):
       self.params = {'force_ops_writes':True}
   pool = context.MutationPool(mapreduce_spec=MapreduceSpec())
   m = mox.Mox()
   m.StubOutWithMock(datastore, 'Put', use_mock_anything=True)
   m.StubOutWithMock(datastore, 'Delete', use_mock_anything=True)
   e1 = TestEntity()
   e2 = TestEntity(key_name='key2')
   self.record_put([e1._populate_internal_entity()], True)
   self.record_delete([e2.key()], True)
   m.ReplayAll()
   try:  # test, verify
     pool.put(e1)
     pool.delete(e2.key())
     self.assertEquals([e1._populate_internal_entity()], pool.puts.items)
     self.assertEquals([e2.key()], pool.deletes.items)
     pool.flush()
     m.VerifyAll()
   finally:
     m.UnsetStubs()
Ejemplo n.º 12
0
  def testDeleteOverPoolSize(self):
    """Test deleting more than pool size."""
    self.pool = context.MutationPool(500)

    m = mox.Mox()
    m.StubOutWithMock(datastore, 'Delete', use_mock_anything=True)

    e1 = TestEntity(key_name='goingaway')
    e2 = TestEntity(key_name='x' * 500)

    # Record Calls
    self.record_delete([e1.key()])

    m.ReplayAll()
    try:  # test, verify
      self.pool.delete(e1)
      self.assertEquals([e1.key()], self.pool.deletes.items)

      self.pool.delete(e2)
      self.assertEquals([e2.key()], self.pool.deletes.items)

      m.VerifyAll()
    finally:
      m.UnsetStubs()
Ejemplo n.º 13
0
  def testPutOverPoolSize(self):
    """Test putting more than pool size."""
    self.pool = context.MutationPool(1000)

    m = mox.Mox()
    m.StubOutWithMock(datastore, 'Put', use_mock_anything=True)

    e1 = TestEntity()
    e2 = TestEntity(tag=' ' * 1000)

    # Record Calls
    self.record_put([e1._populate_internal_entity()])

    m.ReplayAll()
    try:  # test, verify
      self.pool.put(e1)
      self.assertEquals([e1._populate_internal_entity()], self.pool.puts.items)

      self.pool.put(e2)
      self.assertEquals([e2._populate_internal_entity()], self.pool.puts.items)

      m.VerifyAll()
    finally:
      m.UnsetStubs()