Example #1
0
    def test_multiple_threads_unique_http_objects(self):
        """Validate that each thread gets its unique http object.

        At the core of this requirement is the fact that httplib2.Http is not
        thread-safe. Therefore, it is the responsibility of the repo to maintain
        a separate http object even if multiplethreads share it.
        """
        def get_http(repo, result, i):
            result[i] = repo.http

        gcp_service_mock = mock.Mock()
        credentials_mock = mock.Mock(spec=client.Credentials)
        repo = base.GCPRepository(gcp_service=gcp_service_mock,
                                  credentials=credentials_mock,
                                  component='fake_component',
                                  use_cached_http=True)

        http_objects = [None] * 2
        t1 = threading.Thread(target=get_http, args=(repo, http_objects, 0))
        t2 = threading.Thread(target=get_http, args=(repo, http_objects, 1))

        t1.start()
        t2.start()
        t1.join()
        t2.join()

        self.assertNotEqual(http_objects[0], http_objects[1])
Example #2
0
    def test_use_cached_http_gets_same_http_objects(self, signer_factory):
        """Different clients with the same credential get the same http object.

        This verifies that a new http object is not created when two
        repository clients use the same credentials object.
        """
        fake_credentials = self.get_test_credential()

        http_objects = [None] * 2
        for i in range(2):
            gcp_service_mock = mock.Mock()
            repo = base.GCPRepository(gcp_service=gcp_service_mock,
                                      credentials=fake_credentials,
                                      component='fake_component{}'.format(i),
                                      use_cached_http=True)
            http_objects[i] = repo.http

        self.assertEqual(http_objects[0], http_objects[1])
Example #3
0
    def test_no_cached_http_gets_different_http_objects(self, signer_factory):
        """Validate that each unique credential gets a unique http object.

        At the core of this requirement is the fact that some API's require
        distinctly scoped credentials, whereas the authenticated http object
        is cached for all clients in the same thread.
        """
        http_objects = [None] * 2
        for i in range(2):
            gcp_service_mock = mock.Mock()
            fake_credentials = self.get_test_credential()
            repo = base.GCPRepository(gcp_service=gcp_service_mock,
                                      credentials=fake_credentials,
                                      component='fake_component{}'.format(i),
                                      use_cached_http=False)
            http_objects[i] = repo.http

        self.assertNotEqual(http_objects[0], http_objects[1])