Esempio n. 1
0
        public async Task TestServerStoreModelWithNoRequestEventHandler()
        {
            EventHandler <SendingRequestEventArgs>   sendingRequest   = null;
            EventHandler <ReceivedResponseEventArgs> receivedResponse = null;
            ServerStoreModel storeModel = new ServerStoreModel(GetMockStoreClient(), sendingRequest, receivedResponse);

            using (DocumentServiceRequest request = DocumentServiceRequest.Create(
                       OperationType.Read,
                       ResourceType.Document,
                       AuthorizationTokenType.PrimaryMasterKey))
            {
                DocumentServiceResponse result = await storeModel.ProcessMessageAsync(request);

                Assert.IsTrue(request.Headers.Get(newHeaderKey) == null);
            }
        }
        public void MockStoreClientTest()
        {
            // create a real document service request (with auth token level = god)
            DocumentServiceRequest entity = DocumentServiceRequest.Create(OperationType.Read, ResourceType.Document, AuthorizationTokenType.PrimaryMasterKey);

            // set request charge tracker -  this is referenced in store reader (ReadMultipleReplicaAsync)
            var requestContext = new DocumentServiceRequestContext();

            requestContext.RequestChargeTracker = new RequestChargeTracker();
            entity.RequestContext = requestContext;

            // set a dummy resource id on the request.
            entity.ResourceId = "1-MxAPlgMgA=";

            // set consistency level on the request to Bounded Staleness
            entity.Headers[HttpConstants.HttpHeaders.ConsistencyLevel] = ConsistencyLevel.BoundedStaleness.ToString();

            // also setup timeout helper, used in store reader
            entity.RequestContext.TimeoutHelper = new TimeoutHelper(new TimeSpan(2, 2, 2));

            // when the store reader throws Invalid Partition exception, the higher layer should
            // clear this target identity.
            entity.RequestContext.TargetIdentity            = new ServiceIdentity("dummyTargetIdentity1", new Uri("http://dummyTargetIdentity1"), false);
            entity.RequestContext.ResolvedPartitionKeyRange = new PartitionKeyRange();

            AddressInformation[] addressInformation = GetMockAddressInformationDuringUpgrade();
            var mockAddressCache = GetMockAddressCache(addressInformation);

            // validate that the mock works
            PartitionAddressInformation partitionAddressInformation = mockAddressCache.Object.ResolveAsync(entity, false, new CancellationToken()).Result;
            var addressInfo = partitionAddressInformation.AllAddresses;

            Assert.IsTrue(addressInfo[0] == addressInformation[0]);

            AddressSelector addressSelector = new AddressSelector(mockAddressCache.Object, Protocol.Tcp);
            var             primaryAddress  = addressSelector.ResolvePrimaryUriAsync(entity, false /*forceAddressRefresh*/).Result;

            // check if the address return from Address Selector matches the original address info
            Assert.IsTrue(primaryAddress.Equals(addressInformation[0].PhysicalUri));

            // get mock transport client that returns a sequence of responses to simulate upgrade
            var mockTransportClient = GetMockTransportClientDuringUpgrade(addressInformation);

            // get response from mock object
            var response = mockTransportClient.InvokeResourceOperationAsync(new Uri(addressInformation[0].PhysicalUri), entity).Result;

            // validate that the LSN matches
            Assert.IsTrue(response.LSN == 50);

            string activityId;

            response.TryGetHeaderValue(WFConstants.BackendHeaders.ActivityId, out activityId);

            // validate that the ActivityId Matches
            Assert.IsTrue(activityId == "ACTIVITYID1_1");

            // create a real session container - we don't need session for this test anyway
            ISessionContainer sessionContainer = new SessionContainer(string.Empty);

            // create store reader with mock transport client, real address selector (that has mock address cache), and real session container
            StoreReader storeReader =
                new StoreReader(mockTransportClient,
                                addressSelector,
                                sessionContainer);

            Mock <IAuthorizationTokenProvider> mockAuthorizationTokenProvider = new Mock <IAuthorizationTokenProvider>();

            // setup max replica set size on the config reader
            ReplicationPolicy replicationPolicy = new ReplicationPolicy();

            replicationPolicy.MaxReplicaSetSize = 4;
            Mock <IServiceConfigurationReader> mockServiceConfigReader = new Mock <IServiceConfigurationReader>();

            mockServiceConfigReader.SetupGet(x => x.UserReplicationPolicy).Returns(replicationPolicy);

            try
            {
                StoreClient storeClient =
                    new StoreClient(
                        mockAddressCache.Object,
                        sessionContainer,
                        mockServiceConfigReader.Object,
                        mockAuthorizationTokenProvider.Object,
                        Protocol.Tcp,
                        mockTransportClient);

                ServerStoreModel storeModel = new ServerStoreModel(storeClient);

                var result = storeModel.ProcessMessageAsync(entity).Result;

                // if we have reached this point, there was a successful request.
                // validate if the target identity has been cleared out.
                // If the target identity is null and the request still succeeded, it means
                // that the very first read succeeded without a barrier request.
                Assert.IsNull(entity.RequestContext.TargetIdentity);
                Assert.IsNull(entity.RequestContext.ResolvedPartitionKeyRange);
            }
            catch (Exception e)
            {
                Assert.IsTrue(e.InnerException is ServiceUnavailableException ||
                              e.InnerException is ArgumentNullException ||
                              e.InnerException is ServiceUnavailableException ||
                              e.InnerException is NullReferenceException);
            }
        }