Ejemplo n.º 1
0
        public static Mock <IMediaDataServiceContext> CreateSaveChangesMock <T>(Exception fakeException, int failCount, BaseEntity <T> returnedData)
        {
            var dataContextMock = new Mock <IMediaDataServiceContext>();
            var fakeResponse    = new TestMediaDataServiceResponse {
                AsyncState = returnedData
            };
            int exceptionCount = failCount;

            dataContextMock.Setup((ctxt) => ctxt
                                  .SaveChangesAsync(It.IsAny <object>()))
            .Returns(() => Task.Factory.StartNew <IMediaDataServiceResponse>(() =>
            {
                if (--exceptionCount > 0)
                {
                    throw fakeException;
                }
                return(fakeResponse);
            }));

            dataContextMock.Setup((ctxt) => ctxt
                                  .SaveChanges())
            .Returns(() =>
            {
                if (--exceptionCount > 0)
                {
                    throw fakeException;
                }
                return(fakeResponse);
            });

            return(dataContextMock);
        }
Ejemplo n.º 2
0
        private Func <object, IMediaDataServiceResponse> SaveChangesFunc(object state)
        {
            return((object c) =>
            {
                if (_delaymilliseconds > 0)
                {
                    Thread.Sleep(_delaymilliseconds);
                }
                lock (_lock)
                {
                    foreach (var pendingChange in _pendingChanges)
                    {
                        if (_persistedChanges.ContainsKey(pendingChange.Key))
                        {
                            var addRamgeMethodInfo = _persistedChanges[pendingChange.Key].GetType().GetMethods().Where(m => m.Name == "AddRange").FirstOrDefault();
                            if (addRamgeMethodInfo != null)
                            {
                                addRamgeMethodInfo.Invoke(_persistedChanges[pendingChange.Key], new[] { pendingChange.Value });
                            }
                        }
                        else
                        {
                            _persistedChanges.Add(pendingChange.Key, pendingChange.Value);
                        }
                    }
                    _pendingChanges.Clear();
                }
                IMediaDataServiceResponse response = new TestMediaDataServiceResponse();
                if (state != null)
                {
                    response.AsyncState = state;
                    if (state.GetType().GetProperty("Id") != null)
                    {
                        state.GetType().InvokeMember("Id", BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty, Type.DefaultBinder, state, new[] { "nb:kid:UUID:" + Guid.NewGuid() });
                    }
                    if (state is IMediaContextContainer)
                    {
                        ((IMediaContextContainer)state).SetMediaContext(_mediaContextBase);
                    }

                    if (state is LocatorData)
                    {
                        ((LocatorData)state).BaseUri = "http://contoso.com/" + Guid.NewGuid().ToString();
                        ((LocatorData)state).Path = "http://contoso.com/" + Guid.NewGuid().ToString();
                        ((LocatorData)state).ContentAccessComponent = Guid.NewGuid().ToString();
                    }
                    if (state is AssetData)
                    {
                        ((AssetData)state).Uri = "http://contoso.com/" + Guid.NewGuid().ToString();
                    }
                }
                return response;
            });
        }
        public void TestAssetFileUpdateRetry()
        {
            var dataContextMock = new Mock<IMediaDataServiceContext>();

            int exceptionCount = 2;

            var asset = new AssetData { Name = "testData"};
            var file = new AssetFileData { Name = "testData"};
            var fakeResponse = new TestMediaDataServiceResponse { AsyncState = file };
            var fakeException = new WebException("test", WebExceptionStatus.ConnectionClosed);

            dataContextMock.Setup((ctxt) => ctxt.AttachTo("Files", file));
            dataContextMock.Setup((ctxt) => ctxt.UpdateObject(file));

            dataContextMock.Setup((ctxt) => ctxt
                .SaveChangesAsync(file))
                .Returns(() => Task.Factory.StartNew<IMediaDataServiceResponse>(() =>
                {
                    if (--exceptionCount > 0) throw fakeException;
                    return fakeResponse;
                }));

            _mediaContext.MediaServicesClassFactory = new TestMediaServicesClassFactory(dataContextMock.Object);

            file.SetMediaContext(_mediaContext);
            SetFileAsset(file, asset);

            file.Update();

            Assert.AreEqual(0, exceptionCount);
        }
        public void TestAssetFileCreateRetry()
        {
            var dataContextMock = new Mock<IMediaDataServiceContext>();

            int exceptionCount = 2;

            var expected = new AssetFileData { Name = "testData" };
            var fakeResponse = new TestMediaDataServiceResponse { AsyncState = expected };
            var fakeException = new WebException("testException", WebExceptionStatus.ConnectionClosed);

            dataContextMock.Setup((ctxt) => ctxt.AddObject("Files", It.IsAny<object>()));
            dataContextMock.Setup((ctxt) => ctxt
                .SaveChangesAsync(It.IsAny<object>()))
                .Returns(() => Task.Factory.StartNew<IMediaDataServiceResponse>(() =>
                {
                    if (--exceptionCount > 0) throw fakeException;
                    return fakeResponse;
                }));

            _mediaContext.MediaServicesClassFactory = new TestMediaServicesClassFactory(dataContextMock.Object);

            var asset = new AssetData { Name = "testData" };

            asset.SetMediaContext(_mediaContext);
            IAssetFile file = ((IAsset)asset).AssetFiles.Create("test");
            Assert.AreEqual(expected.Name, file.Name);
            Assert.AreEqual(0, exceptionCount);
        }
        public void JobTemplateTestSaveAsyncRetry()
        {
            JobTemplateData data = new JobTemplateData { JobTemplateBodyCopied = "" };

            var fakeResponse = new TestMediaDataServiceResponse { AsyncState = data };
            var fakeException = new WebException("test", WebExceptionStatus.ConnectionClosed);

            var dataContextMock = new Mock<IMediaDataServiceContext>();

            dataContextMock.Setup((ctxt) => ctxt.AttachTo("Jobs", data));
            dataContextMock.Setup((ctxt) => ctxt.DeleteObject(data));

            int saveChangesExceptionCount = 2;

            dataContextMock.Setup((ctxt) => ctxt
                .SaveChangesAsync(SaveChangesOptions.Batch, data))
                .Returns(() => Task.Factory.StartNew<IMediaDataServiceResponse>(() =>
                {
                    if (--saveChangesExceptionCount > 0) throw fakeException;
                    return fakeResponse;
                }));

            int loadPropertiesExceptionCount = 2;
            dataContextMock.Setup((ctxt) => ctxt
                .LoadProperty(data, It.IsAny<string>()))
                .Returns(() =>
                {
                    if (--loadPropertiesExceptionCount > 0) throw fakeException;
                    return null;
                });

            _mediaContext.MediaServicesClassFactory = new TestMediaServicesClassFactory(dataContextMock.Object);

            data.SetMediaContext(_mediaContext);

            data.SaveAsync().Wait();

            dataContextMock.Verify((ctxt) => ctxt.LoadProperty(data, "TaskTemplates"), Times.Exactly(2));
            Assert.AreEqual(0, saveChangesExceptionCount);
            Assert.AreEqual(0, loadPropertiesExceptionCount);
        }
        public void TestLocatorUpdateRetry()
        {
            var dataContextMock = new Mock<IMediaDataServiceContext>();

            int exceptionCount = 2;

            var locator = new LocatorData { Name = "testData", Type = (int)LocatorType.OnDemandOrigin };
            var fakeResponse = new TestMediaDataServiceResponse { AsyncState = locator };
            var fakeException = new WebException("test", WebExceptionStatus.ConnectionClosed);

            dataContextMock.Setup((ctxt) => ctxt.AttachTo("Locators", locator));
            dataContextMock.Setup((ctxt) => ctxt.UpdateObject(locator));

            dataContextMock.Setup((ctxt) => ctxt
                .SaveChangesAsync(locator))
                .Returns(() => Task.Factory.StartNew<IMediaDataServiceResponse>(() =>
                {
                    if (--exceptionCount > 0) throw fakeException;
                    return fakeResponse;
                }));

            _mediaContext.MediaServicesClassFactory = new TestMediaServicesClassFactory(dataContextMock.Object);

            locator.SetMediaContext(_mediaContext);

            locator.Update(DateTime.Now);

            Assert.AreEqual(0, exceptionCount);
        }
        public void TestLocatorDeleteRetry()
        {
            var dataContextMock = new Mock<IMediaDataServiceContext>();

            int exceptionCount = 2;

            var locator = new LocatorData { Name = "testData", Type = (int)LocatorType.OnDemandOrigin };
            var fakeResponse = new TestMediaDataServiceResponse { AsyncState = locator };
            var fakeException = new WebException("test", WebExceptionStatus.ConnectionClosed);

            dataContextMock.Setup((ctxt) => ctxt.AttachTo("Locators", locator));
            dataContextMock.Setup((ctxt) => ctxt.DeleteObject(locator));

            dataContextMock.Setup((ctxt) => ctxt
                .SaveChangesAsync(locator))
                .Returns(() => Task.Factory.StartNew<IMediaDataServiceResponse>(() =>
                {
                    if (--exceptionCount > 0) throw fakeException;
                    return fakeResponse;
                }));

            // Cannot mock DataServiceQuery. Throw artificial exception to mark pass through saving changes.
            string artificialExceptionMessage = "artificialException";
            dataContextMock.Setup((ctxt) => ctxt.CreateQuery<IAsset, AssetData>("Assets")).Throws(new Exception(artificialExceptionMessage));

            _mediaContext.MediaServicesClassFactory = new TestMediaServicesClassFactory(dataContextMock.Object);

            locator.SetMediaContext(_mediaContext);

            try
            {
                locator.Delete();
            }
            catch (Exception x)
            {
                Assert.AreEqual(artificialExceptionMessage, x.Message);
            }

            Assert.AreEqual(0, exceptionCount);
        }
        public void TestJobUpdateRetry()
        {
            var dataContextMock = new Mock<IMediaDataServiceContext>();

             int exceptionCount = 2;

             var job = new JobData { Name = "testData", Id = "id:someid" };
             var fakeResponse = new TestMediaDataServiceResponse { AsyncState = job };
             var fakeException = new WebException("test", WebExceptionStatus.ConnectionClosed);

             dataContextMock.Setup((ctxt) => ctxt.AttachTo("Jobs", job));
             dataContextMock.Setup((ctxt) => ctxt.DeleteObject(job));

             dataContextMock.Setup((ctxt) => ctxt
                 .SaveChangesAsync(job))
                 .Returns(() => Task.Factory.StartNew<IMediaDataServiceResponse>(() =>
                 {
                     if (--exceptionCount > 0) throw fakeException;
                     return fakeResponse;
                 }));

             _mediaContext.MediaServicesClassFactory = new TestMediaServicesClassFactory(dataContextMock.Object);

             job.SetMediaContext(_mediaContext);

             job.Update();

             Assert.AreEqual(0, exceptionCount);
        }
        public void TestAccessPolicyCreateRetry()
        {
            var dataContextMock = new Mock<IMediaDataServiceContext>();

            int exceptionCount = 2;

            var expected = new AccessPolicyData { Name = "testData" };
            var fakeResponse = new TestMediaDataServiceResponse { AsyncState = expected };
            var fakeException = new WebException("testException", WebExceptionStatus.ConnectionClosed);

            dataContextMock.Setup((ctxt) => ctxt.AddObject("AccessPolicies", It.IsAny<object>()));
            dataContextMock.Setup((ctxt) => ctxt
                .SaveChangesAsync(It.IsAny<object>()))
                .Returns(() => Task.Factory.StartNew<IMediaDataServiceResponse>(() =>
                {
                    if (--exceptionCount > 0) throw fakeException;
                    return fakeResponse;
                }));

            _mediaContext.MediaServicesClassFactory = new TestMediaServicesClassFactory(dataContextMock.Object);

            IAccessPolicy policy = _mediaContext.AccessPolicies.Create("Empty", TimeSpan.FromSeconds(1), AccessPermissions.None);
            Assert.AreEqual(expected.Name, policy.Name);
            Assert.AreEqual(0, exceptionCount);
        }