def parse_session_token(response_headers): """ Extracts session token from response headers and parses :param dict response_headers: :return: A dictionary of partition id to session lsn for given collection :rtype: dict """ # extract session token from response header session_token = '' if http_constants.HttpHeaders.SessionToken in response_headers: session_token = response_headers[http_constants.HttpHeaders.SessionToken] id_to_sessionlsn = {} if session_token is not '': ''' extract id, lsn from the token. For p-collection, the token will be a concatenation of pairs for each collection''' token_pairs = session_token.split(',') for token_pair in token_pairs: tokens = token_pair.split(':') if (len(tokens) == 2): id = tokens[0] sessionToken = VectorSessionToken.create(tokens[1]) if sessionToken is None: raise HTTPFailure(http_constants.StatusCodes.INTERNAL_SERVER_ERROR, "Could not parse the received session token: %s" % tokens[1]) id_to_sessionlsn[id] = sessionToken return id_to_sessionlsn
def parse_session_token(response_headers): """ Extracts session token from response headers and parses :param dict response_headers: :return: A dictionary of partition id to session lsn for given collection :rtype: dict """ # extract session token from response header session_token = '' if http_constants.HttpHeaders.SessionToken in response_headers: session_token = response_headers[http_constants.HttpHeaders.SessionToken] id_to_sessionlsn = {} if session_token is not '': ''' extract id, lsn from the token. For p-collection, the token will be a concatenation of pairs for each collection''' token_pairs = session_token.split(',') for token_pair in token_pairs: tokens = token_pair.split(':') id = tokens[0] sessionToken = VectorSessionToken.create(tokens[1]) id_to_sessionlsn[id] = sessionToken return id_to_sessionlsn
def test_validate_session_token_comparison(self): #valid session token session_token1 = VectorSessionToken.create("1#100#1=20#2=5#3=30") session_token2 = VectorSessionToken.create("2#105#4=10#2=5#3=30") self.assertIsNotNone(session_token1) self.assertIsNotNone(session_token2) self.assertFalse(session_token1.equals(session_token2)) self.assertFalse(session_token2.equals(session_token1)) session_token_merged = VectorSessionToken.create("2#105#2=5#3=30#4=10") self.assertIsNotNone(session_token_merged) self.assertTrue( session_token1.merge(session_token2).equals(session_token_merged)) session_token1 = VectorSessionToken.create("1#100#1=20#2=5#3=30") session_token2 = VectorSessionToken.create("1#100#1=10#2=8#3=30") self.assertIsNotNone(session_token1) self.assertIsNotNone(session_token2) self.assertFalse(session_token1.equals(session_token2)) self.assertFalse(session_token2.equals(session_token1)) session_token_merged = VectorSessionToken.create("1#100#1=20#2=8#3=30") self.assertIsNotNone(session_token_merged) self.assertTrue( session_token_merged.equals(session_token1.merge(session_token2))) session_token1 = VectorSessionToken.create("1#100#1=20#2=5#3=30") session_token2 = VectorSessionToken.create("1#102#1=100#2=8#3=30") self.assertIsNotNone(session_token1) self.assertIsNotNone(session_token1) self.assertFalse(session_token1.equals(session_token2)) self.assertFalse(session_token2.equals(session_token1)) session_token_merged = VectorSessionToken.create( "1#102#2=8#3=30#1=100") self.assertIsNotNone(session_token_merged) self.assertTrue( session_token_merged.equals(session_token1.merge(session_token2))) session_token1 = VectorSessionToken.create("1#101#1=20#2=5#3=30") session_token2 = VectorSessionToken.create("1#100#1=20#2=5#3=30#4=40") self.assertIsNotNone(session_token1) self.assertIsNotNone(session_token2) try: session_token1.merge(session_token2) self.fail( "Region progress can not be different when version is same") except CosmosError as e: self.assertEquals( str(e), "Status Code: 500. Compared session tokens '1#101#1=20#2=5#3=30' and '1#100#1=20#2=5#3=30#4=40' have unexpected regions." )
def test_validate_session_token_parsing_from_empty_string(self): session_token = "" self.assertIsNone(VectorSessionToken.create(session_token))
def test_validate_session_token_parsing_with_invalid_format(self): session_token = "1;100#1=20#2=40" self.assertIsNone(VectorSessionToken.create(session_token))
def test_validate_session_token_parsing_with_invalid_region_progress(self): session_token = "1#100#1=20#2=x#3=30" self.assertIsNone(VectorSessionToken.create(session_token))
def test_validate_session_token_parsing_with_invalid_global_lsn(self): session_token = "1#foo#1=20#2=5#3=30" self.assertIsNone(VectorSessionToken.create(session_token))
def test_validate_successful_session_token_parsing(self): #valid session token session_token = "1#100#1=20#2=5#3=30" self.assertEquals( VectorSessionToken.create(session_token).convert_to_string(), "1#100#1=20#2=5#3=30")
def test_validate_session_token_comparison(self): #valid session token session_token1 = VectorSessionToken.create("1#100#1=20#2=5#3=30") session_token2 = VectorSessionToken.create("2#105#4=10#2=5#3=30") self.assertIsNotNone(session_token1) self.assertIsNotNone(session_token2) self.assertFalse(session_token1.equals(session_token2)) self.assertFalse(session_token2.equals(session_token1)) session_token_merged = VectorSessionToken.create("2#105#2=5#3=30#4=10") self.assertIsNotNone(session_token_merged) self.assertTrue(session_token1.merge(session_token2).equals(session_token_merged)) session_token1 = VectorSessionToken.create("1#100#1=20#2=5#3=30") session_token2 = VectorSessionToken.create("1#100#1=10#2=8#3=30") self.assertIsNotNone(session_token1) self.assertIsNotNone(session_token2) self.assertFalse(session_token1.equals(session_token2)) self.assertFalse(session_token2.equals(session_token1)) session_token_merged = VectorSessionToken.create("1#100#1=20#2=8#3=30") self.assertIsNotNone(session_token_merged) self.assertTrue(session_token_merged.equals(session_token1.merge(session_token2))) session_token1 = VectorSessionToken.create("1#100#1=20#2=5#3=30") session_token2 = VectorSessionToken.create("1#102#1=100#2=8#3=30") self.assertIsNotNone(session_token1) self.assertIsNotNone(session_token1) self.assertFalse(session_token1.equals(session_token2)) self.assertFalse(session_token2.equals(session_token1)) session_token_merged = VectorSessionToken.create("1#102#2=8#3=30#1=100") self.assertIsNotNone(session_token_merged) self.assertTrue(session_token_merged.equals(session_token1.merge(session_token2))) session_token1 = VectorSessionToken.create("1#101#1=20#2=5#3=30") session_token2 = VectorSessionToken.create("1#100#1=20#2=5#3=30#4=40") self.assertIsNotNone(session_token1) self.assertIsNotNone(session_token2) try: session_token1.merge(session_token2) self.fail("Region progress can not be different when version is same") except CosmosError as e: self.assertEquals(str(e), "Status Code: 500. Compared session tokens '1#101#1=20#2=5#3=30' and '1#100#1=20#2=5#3=30#4=40' have unexpected regions.")
def test_validate_successful_session_token_parsing(self): #valid session token session_token = "1#100#1=20#2=5#3=30" self.assertEquals(VectorSessionToken.create(session_token).convert_to_string(), "1#100#1=20#2=5#3=30")