Esempio n. 1
0
        public async Task RetryLogic_Success_Test()
        {
            //Arrange
            Customer cust         = new Customer();
            Guid     customerGuid = Guid.NewGuid();

            _repo.Setup(p => p(It.IsAny <Guid>())).ReturnsAsync(cust);

            //Act
            var res = await _retryPolicy.Do(() => { return(_repo.Object(customerGuid)); });

            //Assert

            Assert.IsNotNull(res);
        }
        /**********************************************************************************/

        private async Task RequestDiscoveryTask(string hubUrl)
        {
            Exception lastException = null;

            try
            {
                Logger.Info($"Terminal {_activityStore.Terminal.Name} is requesting discovery for endpoint '{_activityStore.Terminal.Endpoint}' from Hub at '{hubUrl}' ");

                var response = await _hubDiscoveryRetryPolicy.Do(() => _restfulServiceClient.PostAsync <TerminalDTO, ResponseMessageDTO>(new Uri(string.Concat(hubUrl, _apiSuffix, "/terminals/forceDiscover")), _activityStore.Terminal, (string)null));

                if (!string.IsNullOrWhiteSpace(response?.ErrorCode))
                {
                    lastException = new Exception(response.Message);
                }
            }
            catch (Exception ex)
            {
                lastException = ex;
            }

            if (lastException != null)
            {
                lock (_hubSecrets)
                {
                    TaskCompletionSource <string> setSecretTask;

                    if (_hubSecrets.TryGetValue(hubUrl, out setSecretTask))
                    {
                        Logger.Error($"Terminal {_activityStore.Terminal.Name}({_activityStore.Terminal.Version}): Hub at '{hubUrl}' refused to call terminal discovery endpoint: {lastException.Message}");
                        setSecretTask.TrySetException(new Exception($"Failed to request discovery from the Hub at : {hubUrl}", lastException));
                    }
                }

                UnsubscribeFromHub(hubUrl);
            }
            else
            {
#pragma warning disable 4014
                Task.Run(async() =>
                {
                    bool shouldUnubscribe = false;

                    await Task.Delay(5000);

                    lock (_hubSecrets)
                    {
                        TaskCompletionSource <string> setSecretTask;

                        if (_hubSecrets.TryGetValue(hubUrl, out setSecretTask))
                        {
                            shouldUnubscribe = setSecretTask.TrySetException(new Exception($"Terminal { _activityStore.Terminal.Name }({ _activityStore.Terminal.Version}): Hub '{hubUrl}' failed to respond with discovery request within a given period of time."));
                        }
                    }

                    if (shouldUnubscribe)
                    {
                        Logger.Error($"Terminal { _activityStore.Terminal.Name }({ _activityStore.Terminal.Version}): Hub at '{hubUrl}'failed to respond with discovery request within a given period of time");
                        UnsubscribeFromHub(hubUrl);
                    }
                });
#pragma warning restore 4014
            }
        }