示例#1
0
        public void TracksEntitySyncErrorInCaseOfFailure(Type entityType)
        {
            var exception       = new Exception("SomeRandomMessage");
            var entity          = (IThreadSafeTestModel)Substitute.For(new[] { entityType }, new object[0]);
            var state           = new DeleteEntityState <ITestModel, IDatabaseTestModel, IThreadSafeTestModel>(api, analyticsService, dataSource, LeakyBucket, RateLimiter);
            var expectedMessage = $"{Delete}:{exception.Message}";
            var analyticsEvent  = entity.GetType().ToSyncErrorAnalyticsEvent(analyticsService);

            PrepareApiCallFunctionToThrow(exception);

            state.Start(entity).Wait();

            analyticsEvent.Received().Track(expectedMessage);
        }
        private static IStateResult configurePush <TModel, TDatabase, TThreadsafe>(
            ITransitionConfigurator transitions,
            IStateResult entryPoint,
            IDataSource <TThreadsafe, TDatabase> dataSource,
            IAnalyticsService analyticsService,
            ICreatingApiClient <TModel> creatingApi,
            IUpdatingApiClient <TModel> updatingApi,
            IDeletingApiClient <TModel> deletingApi,
            Func <TModel, TThreadsafe> toClean,
            Func <TThreadsafe, string, TThreadsafe> toUnsyncable,
            ITogglApi api,
            IRetryDelayService apiDelay,
            IScheduler scheduler,
            IObservable <Unit> delayCancellation)
            where TModel : class, IIdentifiable, ILastChangedDatable
            where TDatabase : class, TModel, IDatabaseSyncable
            where TThreadsafe : class, TDatabase, IThreadSafeModel
        {
            var rnd         = new Random();
            var statusDelay = new RetryDelayService(rnd);

            var push                  = new PushState <TDatabase, TThreadsafe>(dataSource);
            var pushOne               = new PushOneEntityState <TThreadsafe>();
            var create                = new CreateEntityState <TModel, TThreadsafe>(creatingApi, dataSource, analyticsService, toClean);
            var update                = new UpdateEntityState <TModel, TThreadsafe>(updatingApi, dataSource, analyticsService, toClean);
            var delete                = new DeleteEntityState <TModel, TDatabase, TThreadsafe>(deletingApi, dataSource, analyticsService);
            var deleteLocal           = new DeleteLocalEntityState <TDatabase, TThreadsafe>(dataSource);
            var tryResolveClientError = new TryResolveClientErrorState <TThreadsafe>();
            var unsyncable            = new UnsyncableEntityState <TThreadsafe>(dataSource, toUnsyncable);
            var checkServerStatus     = new CheckServerStatusState(api, scheduler, apiDelay, statusDelay, delayCancellation);
            var finished              = new ResetAPIDelayState(apiDelay);

            transitions.ConfigureTransition(entryPoint, push);
            transitions.ConfigureTransition(push.PushEntity, pushOne);
            transitions.ConfigureTransition(pushOne.CreateEntity, create);
            transitions.ConfigureTransition(pushOne.UpdateEntity, update);
            transitions.ConfigureTransition(pushOne.DeleteEntity, delete);
            transitions.ConfigureTransition(pushOne.DeleteEntityLocally, deleteLocal);

            transitions.ConfigureTransition(create.ClientError, tryResolveClientError);
            transitions.ConfigureTransition(update.ClientError, tryResolveClientError);
            transitions.ConfigureTransition(delete.ClientError, tryResolveClientError);

            transitions.ConfigureTransition(create.ServerError, checkServerStatus);
            transitions.ConfigureTransition(update.ServerError, checkServerStatus);
            transitions.ConfigureTransition(delete.ServerError, checkServerStatus);

            transitions.ConfigureTransition(create.UnknownError, checkServerStatus);
            transitions.ConfigureTransition(update.UnknownError, checkServerStatus);
            transitions.ConfigureTransition(delete.UnknownError, checkServerStatus);

            transitions.ConfigureTransition(tryResolveClientError.UnresolvedTooManyRequests, checkServerStatus);
            transitions.ConfigureTransition(tryResolveClientError.Unresolved, unsyncable);

            transitions.ConfigureTransition(checkServerStatus.Retry, checkServerStatus);
            transitions.ConfigureTransition(checkServerStatus.ServerIsAvailable, push);

            transitions.ConfigureTransition(create.CreatingFinished, finished);
            transitions.ConfigureTransition(update.UpdatingSucceeded, finished);
            transitions.ConfigureTransition(delete.DeletingFinished, finished);
            transitions.ConfigureTransition(deleteLocal.Deleted, finished);
            transitions.ConfigureTransition(deleteLocal.DeletingFailed, finished);

            transitions.ConfigureTransition(finished.Continue, push);

            return(push.NothingToPush);
        }
        private static LookForChangeToPushState <TDatabase, TThreadsafe> configurePush <TModel, TDatabase, TThreadsafe>(
            ITransitionConfigurator transitions,
            IStateResult entryPoint,
            IDataSource <TThreadsafe, TDatabase> dataSource,
            IAnalyticsService analyticsService,
            ICreatingApiClient <TModel> creatingApi,
            IUpdatingApiClient <TModel> updatingApi,
            IDeletingApiClient <TModel> deletingApi,
            ILeakyBucket minutesLeakyBucket,
            IRateLimiter rateLimiter,
            WaitForAWhileState waitForAWhileState,
            Func <TModel, TThreadsafe> toClean,
            Func <TThreadsafe, string, TThreadsafe> toUnsyncable)
            where TModel : class, IIdentifiable, ILastChangedDatable
            where TDatabase : class, TModel, IDatabaseSyncable
            where TThreadsafe : class, TDatabase, IThreadSafeModel
        {
            var lookForChange      = new LookForChangeToPushState <TDatabase, TThreadsafe>(dataSource);
            var chooseOperation    = new ChooseSyncOperationState <TThreadsafe>();
            var create             = new CreateEntityState <TModel, TDatabase, TThreadsafe>(creatingApi, dataSource, analyticsService, minutesLeakyBucket, rateLimiter, toClean);
            var update             = new UpdateEntityState <TModel, TThreadsafe>(updatingApi, dataSource, analyticsService, minutesLeakyBucket, rateLimiter, toClean);
            var delete             = new DeleteEntityState <TModel, TDatabase, TThreadsafe>(deletingApi, analyticsService, dataSource, minutesLeakyBucket, rateLimiter);
            var deleteLocal        = new DeleteLocalEntityState <TDatabase, TThreadsafe>(dataSource);
            var processClientError = new ProcessClientErrorState <TThreadsafe>();
            var unsyncable         = new MarkEntityAsUnsyncableState <TThreadsafe>(dataSource, toUnsyncable);

            transitions.ConfigureTransition(entryPoint, lookForChange);
            transitions.ConfigureTransition(lookForChange.ChangeFound, chooseOperation);
            transitions.ConfigureTransition(chooseOperation.CreateEntity, create);
            transitions.ConfigureTransition(chooseOperation.UpdateEntity, update);
            transitions.ConfigureTransition(chooseOperation.DeleteEntity, delete);
            transitions.ConfigureTransition(chooseOperation.DeleteEntityLocally, deleteLocal);

            transitions.ConfigureTransition(create.ClientError, processClientError);
            transitions.ConfigureTransition(update.ClientError, processClientError);
            transitions.ConfigureTransition(delete.ClientError, processClientError);

            transitions.ConfigureTransition(create.ServerError, new FailureState());
            transitions.ConfigureTransition(update.ServerError, new FailureState());
            transitions.ConfigureTransition(delete.ServerError, new FailureState());

            transitions.ConfigureTransition(create.UnknownError, new FailureState());
            transitions.ConfigureTransition(update.UnknownError, new FailureState());
            transitions.ConfigureTransition(delete.UnknownError, new FailureState());

            transitions.ConfigureTransition(processClientError.UnresolvedTooManyRequests, new FailureState());
            transitions.ConfigureTransition(processClientError.Unresolved, unsyncable);

            transitions.ConfigureTransition(create.EntityChanged, lookForChange);
            transitions.ConfigureTransition(update.EntityChanged, lookForChange);

            transitions.ConfigureTransition(create.PreventOverloadingServer, waitForAWhileState);
            transitions.ConfigureTransition(update.PreventOverloadingServer, waitForAWhileState);
            transitions.ConfigureTransition(delete.PreventOverloadingServer, waitForAWhileState);

            transitions.ConfigureTransition(create.Done, lookForChange);
            transitions.ConfigureTransition(update.Done, lookForChange);
            transitions.ConfigureTransition(delete.Done, lookForChange);
            transitions.ConfigureTransition(deleteLocal.Done, lookForChange);
            transitions.ConfigureTransition(deleteLocal.DeletingFailed, lookForChange);
            transitions.ConfigureTransition(unsyncable.Done, lookForChange);

            return(lookForChange);
        }