public void SchedulerHandlesJobWatcherObjectDisposedExceptionByStopping(bool throwSecondExceptionInDispose) { // The mock job watcher will throw ObjectDisposedException on the first // call to GetNextJobToProcess. This should cause the scheduler's job watching // thread to begin shutting down and eventually call the job watcher's Dispose. // That will wake us up from sleep so we can verify that the scheduler has stopped // on its own. // We also check what happens if a second exception occurs in Dispose during shutdown. IJobWatcher mockJobWatcher = Mocks.CreateMock <IJobWatcher>(); Expect.Call(mockJobWatcher.GetNextJobToProcess()).Throw(new ObjectDisposedException("Uh oh!")); mockJobWatcher.Dispose(); LastCall.Do((DisposeDelegate) delegate { Wake(); if (throwSecondExceptionInDispose) { throw new Exception("Yikes! We're trying to shut down here!"); } }); Expect.Call(mockJobStore.CreateJobWatcher(scheduler.Guid)).Return(mockJobWatcher); Mocks.ReplayAll(); scheduler.Start(); WaitUntilWake(); Assert.IsFalse(scheduler.IsRunning, "The scheduler should have stopped itself automatically."); }
public static void RhinoMockReturnsTestValue() { MockRepository rhinoEngine = new MockRepository(); ITestMock testMock = rhinoEngine.DynamicMock <ITestMock>(); using (rhinoEngine.Record()) { testMock.ReturnSomeString(1); LastCall.Return("string1"); testMock.ReturnSomeString(2); LastCall.Throw(new ArgumentException()); int input = 3; testMock.ReturnSomeString(input); LastCall.Do(new TestDelegate((inp) => "string" + inp)); } string result = testMock.ReturnSomeString(1); Assert.AreEqual("string1", result); Assert.Throws <ArgumentException>(() => testMock.ReturnSomeString(2)); result = testMock.ReturnSomeString(3); Assert.AreEqual("string3", result); }
public void ReadObject_Objects_Externalizable_AMF3() { IExternalizable externalizableValue = Mocks.CreateMock <IExternalizable>(); externalizableValue.ReadExternal(input); LastCall.Do((ReadExternalDelegate) delegate(IDataInput inputToUse) { // Note: inputToUse will be the same instance of AMFDataInput which we've already // tested so we don't need to try all combinations here. Just a few as a sanity check. Assert.AreEqual("abc", inputToUse.ReadUTF()); Assert.AreEqual(10, inputToUse.ReadInt()); Assert.AreEqual(new ASString("def"), inputToUse.ReadObject()); }); Expect.Call(serializer.CreateExternalizableInstance("class")).Return(externalizableValue); Mocks.ReplayAll(); SetStreamContents(new byte[] { (byte)AMF0ObjectTypeCode.AMF3Data, (byte)AMF3ObjectTypeCode.Object, 0x07, 0x0b, 0x63, 0x6c, 0x61, 0x73, 0x73, // class def 0x00, 0x03, 0x61, 0x62, 0x63, // write utf "abc" 0x00, 0x00, 0x00, 0x0a, // write int 10 (byte)AMF3ObjectTypeCode.String, 0x07, 0x64, 0x65, 0x66 // write object "def" }); input.BeginObjectStream(); ASExternalizableObject obj = (ASExternalizableObject)input.ReadObject(); Assert.AreEqual(AMFObjectEncoding.AMF3, input.ObjectEncoding); input.EndObjectStream(); Assert.AreEqual("class", obj.Class.ClassAlias); Assert.AreEqual(ASClassLayout.Externalizable, obj.Class.Layout); Assert.AreSame(externalizableValue, obj.ExternalizableValue); }
public void WriteObject_Objects_Externalizable_AMF3() { IExternalizable externalizableValue = Mocks.CreateMock <IExternalizable>(); externalizableValue.WriteExternal(output); LastCall.Do((WriteExternalDelegate) delegate(IDataOutput outputToUse) { // Note: outputToUse will be the same instance as output which we've already // tested so we don't need to try all combinations here. Just a few as a sanity check. outputToUse.WriteUTF("abc"); outputToUse.WriteInt(10); outputToUse.WriteObject(new ASString("def")); }); ASClass @class = new ASClass("class", ASClassLayout.Externalizable, EmptyArray <string> .Instance); ASExternalizableObject obj = new ASExternalizableObject(@class, externalizableValue); Mocks.ReplayAll(); output.ObjectEncoding = AMFObjectEncoding.AMF3; output.BeginObjectStream(); output.WriteObject(obj); output.EndObjectStream(); byte[] expected = new byte[] { (byte)AMF0ObjectTypeCode.AMF3Data, (byte)AMF3ObjectTypeCode.Object, 0x07, 0x0b, 0x63, 0x6c, 0x61, 0x73, 0x73, // class def 0x00, 0x03, 0x61, 0x62, 0x63, // write utf "abc" 0x00, 0x00, 0x00, 0x0a, // write int 10 (byte)AMF3ObjectTypeCode.String, 0x07, 0x64, 0x65, 0x66 // write object "def" }; CollectionAssert.AreElementsEqual(expected, stream.ToArray()); }
/// <summary> /// Schedules a job that is guaranteed to be executed. /// </summary> private void PrepareJobForExecution(BeingExecuteDelegate beginExecute, EndExecuteDelegate endExecute) { JobDetails jobDetails = new JobDetails(dummyJobSpec, DateTime.UtcNow); jobDetails.JobState = JobState.Pending; PrepareMockJobWatcher(jobDetails); Expect.Call(mockTrigger.Schedule(TriggerScheduleCondition.Latch, DateTime.MinValue, null)) .Constraints(Rhino.Mocks.Constraints.Is.Equal(TriggerScheduleCondition.Latch), Rhino.Mocks.Constraints.Is.Anything(), Rhino.Mocks.Constraints.Is.Null()) .Return(TriggerScheduleAction.ExecuteJob); Expect.Call(mockTrigger.NextFireTimeUtc).Return(null); Expect.Call(mockTrigger.NextMisfireThreshold).Return(null); mockJobStore.SaveJobDetails(jobDetails); LastCall.Do((SaveJobDetailsDelegate) delegate { Assert.AreEqual(JobState.Running, jobDetails.JobState); Assert.IsNotNull(jobDetails.LastJobExecutionDetails); Assert.AreEqual(scheduler.Guid, jobDetails.LastJobExecutionDetails.SchedulerGuid); Assert.GreaterOrEqual(jobDetails.LastJobExecutionDetails.StartTimeUtc, jobDetails.CreationTimeUtc); Assert.IsNull(jobDetails.LastJobExecutionDetails.EndTimeUtc); Assert.IsFalse(jobDetails.LastJobExecutionDetails.Succeeded); }); Expect.Call(mockJobRunner.BeginExecute(null, null, null)) .IgnoreArguments().Repeat.Any().Do(beginExecute); Expect.Call(mockJobRunner.EndExecute(null)) .IgnoreArguments().Repeat.Any().Do(endExecute); }
public void ScheduleHandlesTriggerExceptionByStoppingTheTrigger() { JobDetails jobDetails = new JobDetails(dummyJobSpec, DateTime.UtcNow); jobDetails.JobState = JobState.Pending; PrepareMockJobWatcher(jobDetails); Expect.Call(mockTrigger.Schedule(TriggerScheduleCondition.Latch, DateTime.UtcNow, null)) .Constraints(Rhino.Mocks.Constraints.Is.Equal(TriggerScheduleCondition.Latch), Rhino.Mocks.Constraints.Is.Anything(), Rhino.Mocks.Constraints.Is.Null()) .Throw(new Exception("Oh no!")); // throw an exception from the trigger mockJobStore.SaveJobDetails(jobDetails); LastCall.Do((SaveJobDetailsDelegate)WakeOnSaveJobDetails); Mocks.ReplayAll(); RunSchedulerUntilWake(); Assert.AreEqual(JobState.Stopped, jobDetails.JobState); Assert.IsNull(jobDetails.NextTriggerFireTimeUtc); Assert.IsNull(jobDetails.NextTriggerMisfireThreshold); Assert.IsNull(jobDetails.LastJobExecutionDetails); Assert.IsNull(jobDetails.JobSpec.JobData); }
public void SchedulePendingJob_WithSkipAction() { JobDetails jobDetails = new JobDetails(dummyJobSpec, DateTime.UtcNow); jobDetails.JobState = JobState.Pending; PrepareMockJobWatcher(jobDetails); Expect.Call(mockTrigger.Schedule(TriggerScheduleCondition.Latch, DateTime.UtcNow, null)) .Constraints(Rhino.Mocks.Constraints.Is.Equal(TriggerScheduleCondition.Latch), Rhino.Mocks.Constraints.Is.Anything(), Rhino.Mocks.Constraints.Is.Null()) .Return(TriggerScheduleAction.Skip); Expect.Call(mockTrigger.NextFireTimeUtc).Return(new DateTime(1970, 1, 5, 0, 0, 0, DateTimeKind.Utc)); Expect.Call(mockTrigger.NextMisfireThreshold).Return(new TimeSpan(0, 1, 0)); mockJobStore.SaveJobDetails(jobDetails); LastCall.Do((SaveJobDetailsDelegate)WakeOnSaveJobDetails); Mocks.ReplayAll(); RunSchedulerUntilWake(); Assert.AreEqual(JobState.Scheduled, jobDetails.JobState); DateTimeAssert.AreEqualIncludingKind(new DateTime(1970, 1, 5, 0, 0, 0, DateTimeKind.Utc), jobDetails.NextTriggerFireTimeUtc); Assert.AreEqual(new TimeSpan(0, 1, 0), jobDetails.NextTriggerMisfireThreshold); Assert.IsNull(jobDetails.JobSpec.JobData); Assert.IsNull(jobDetails.LastJobExecutionDetails); }
public void RemotingRefString() { string s = ""; remotingTarget.RefStr(ref s); LastCall.Do(new RefStrDel(SayHello)); mocks.ReplayAll(); remotingTarget.RefStr(ref s); Assert.Equal("Hello", s); }
public void RefInt() { int i = 0; target.RefInt(ref i); LastCall.Do(new RefIntDel(RefFive)); mocks.ReplayAll(); target.RefInt(ref i); Assert.Equal(5, i); }
public void OutInt() { int i = 0; target.OutInt(out i); LastCall.Do(new OutIntDel(OutFive)); mocks.ReplayAll(); target.OutInt(out i); Assert.Equal(5, i); }
public void OutString() { string s = ""; target.OutStr(out s); LastCall.Do(new OutStrDel(OutSayHello)); mocks.ReplayAll(); target.OutStr(out s); Assert.Equal("Hello", s); }
public void ModifyOtherObjectInClientTransactionRollingBack() { _order1.DeliveryDate = DateTime.Now; _mockRepository.BackToRecord(_order1MockEventReceiver); _mockRepository.BackToRecord(_clientTransactionExtensionMock); using (_mockRepository.Ordered()) { _clientTransactionExtensionMock.RollingBack(null, null); LastCall.Constraints(Is.Same(TestableClientTransaction), Property.Value("Count", 1) & List.IsIn(_order1)); _clientTransactionMockEventReceiver.RollingBack(_order1); LastCall.Do(new EventHandler <ClientTransactionEventArgs> (ChangeCustomerNameCallback)); _clientTransactionExtensionMock.PropertyValueChanging(TestableClientTransaction, null, null, null, null); LastCall.IgnoreArguments(); _customer1MockEventReceiver.PropertyChanging(null, null); LastCall.IgnoreArguments(); _customer1MockEventReceiver.PropertyChanged(null, null); LastCall.IgnoreArguments(); _clientTransactionExtensionMock.PropertyValueChanged(TestableClientTransaction, null, null, null, null); LastCall.IgnoreArguments(); _order1MockEventReceiver.RollingBack(null, null); LastCall.Constraints(Is.Same(_order1), Is.NotNull()); _clientTransactionExtensionMock.RollingBack(null, null); LastCall.Constraints(Is.Same(TestableClientTransaction), Property.Value("Count", 1) & List.IsIn(_customer1)); _clientTransactionMockEventReceiver.RollingBack(_customer1); _customer1MockEventReceiver.RollingBack(null, null); LastCall.Constraints(Is.Same(_customer1), Is.NotNull()); _customer1MockEventReceiver.RolledBack(null, null); LastCall.Constraints(Is.Same(_customer1), Is.NotNull()); _order1MockEventReceiver.RolledBack(null, null); LastCall.Constraints(Is.Same(_order1), Is.NotNull()); _clientTransactionMockEventReceiver.RolledBack(_order1, _customer1); _clientTransactionExtensionMock.RolledBack(null, null); LastCall.Constraints(Is.Same(TestableClientTransaction), Property.Value("Count", 2) & new ContainsConstraint(_order1, _customer1)); } _mockRepository.ReplayAll(); TestableClientTransaction.Rollback(); _mockRepository.VerifyAll(); }
public void TestLastCall() { MockRepository mocks = new MockRepository(); ICache <string, int> mockCache = mocks.StrictMock <ICache <string, int> >(); mockCache.Add("a", 1); LastCall.Do((Proc <string, int>) delegate { }); mocks.ReplayAll(); mockCache.Add("a", 1); mocks.VerifyAll(); }
public void Invalid_DelegateToGenericMock() { MockRepository mocks = new MockRepository(); IEMailFormatter <string> formatterMock = mocks.StrictMock <IEMailFormatter <string> >(); SmtpEMailSenderBase <string> senderMock = (SmtpEMailSenderBase <string>)mocks.StrictMock(typeof(SmtpEMailSenderBase <string>)); senderMock.SetFormatter(formatterMock); Assert.Throws <InvalidOperationException>("Callback arguments didn't match the method arguments", () => LastCall.Do( (Action <IEMailFormatter <int> >) delegate(IEMailFormatter <int> formatter) { Assert.NotNull(formatter); })); }
public void ChangeOtherObjectBackToOriginalInClientTransactionRollingBack() { _order1.DeliveryDate = DateTime.Now; _customer1.Name = "New customer name"; _mockRepository.BackToRecord(_order1MockEventReceiver); _mockRepository.BackToRecord(_customer1MockEventReceiver); _mockRepository.BackToRecord(_clientTransactionExtensionMock); using (_mockRepository.Ordered()) { _clientTransactionExtensionMock.RollingBack(null, null); LastCall.Constraints(Is.Same(TestableClientTransaction), Property.Value("Count", 2) & new ContainsConstraint(_order1, _customer1)); _clientTransactionMockEventReceiver.RollingBack(_order1, _customer1); LastCall.Do(new EventHandler <ClientTransactionEventArgs> (ChangeCustomerNameBackToOriginalCallback)); _clientTransactionExtensionMock.PropertyValueChanging(TestableClientTransaction, null, null, null, null); LastCall.IgnoreArguments(); _customer1MockEventReceiver.PropertyChanging(null, null); LastCall.IgnoreArguments(); _customer1MockEventReceiver.PropertyChanged(null, null); LastCall.IgnoreArguments(); _clientTransactionExtensionMock.PropertyValueChanged(TestableClientTransaction, null, null, null, null); LastCall.IgnoreArguments(); _order1MockEventReceiver.RollingBack(null, null); LastCall.Constraints(Is.Same(_order1), Is.NotNull()); _customer1MockEventReceiver.RollingBack(null, null); LastCall.Constraints(Is.Same(_customer1), Is.NotNull()); _order1MockEventReceiver.RolledBack(null, null); LastCall.Constraints(Is.Same(_order1), Is.NotNull()); // Note: Customer1 must not raise a RolledBack event, because its Name has been set back to the OriginalValue. _clientTransactionMockEventReceiver.RolledBack(_order1); _clientTransactionExtensionMock.RolledBack(null, null); LastCall.Constraints(Is.Same(TestableClientTransaction), Property.Value("Count", 1) & List.IsIn(_order1)); } _mockRepository.ReplayAll(); TestableClientTransaction.Rollback(); _mockRepository.VerifyAll(); }
public void SchedulerTriggeredJob_WithSkipAction(bool misfire, bool nextTriggerFireTimeNotNull, bool nextTriggerMisfireThresholdNotNull) { // Create a job scheduled to fire 3 minutes in the past. // We cause a misfire by setting a threshold for 2 seconds which clearly is // in the past. Otherwise we set the threshold to 1 minute which clearly is satisfiable. DateTime schedTime = DateTime.UtcNow.AddSeconds(-3); JobDetails jobDetails = new JobDetails(dummyJobSpec, schedTime.AddSeconds(-5)); jobDetails.JobState = JobState.Triggered; if (nextTriggerFireTimeNotNull) { jobDetails.NextTriggerFireTimeUtc = schedTime; } if (nextTriggerMisfireThresholdNotNull) { jobDetails.NextTriggerMisfireThreshold = misfire ? new TimeSpan(0, 0, 2) : new TimeSpan(0, 1, 0); } PrepareMockJobWatcher(jobDetails); TriggerScheduleCondition expectedCondition = misfire ? TriggerScheduleCondition.Misfire : TriggerScheduleCondition.Fire; Expect.Call(mockTrigger.Schedule(expectedCondition, DateTime.MinValue, null)) .Constraints(Rhino.Mocks.Constraints.Is.Equal(expectedCondition), Rhino.Mocks.Constraints.Is.Anything(), Rhino.Mocks.Constraints.Is.Null()) .Return(TriggerScheduleAction.Skip); Expect.Call(mockTrigger.NextFireTimeUtc).Return(new DateTime(1970, 1, 5, 0, 0, 0, DateTimeKind.Utc)); Expect.Call(mockTrigger.NextMisfireThreshold).Return(new TimeSpan(0, 2, 0)); mockJobStore.SaveJobDetails(jobDetails); LastCall.Do((SaveJobDetailsDelegate)WakeOnSaveJobDetails); Mocks.ReplayAll(); RunSchedulerUntilWake(); Assert.AreEqual(JobState.Scheduled, jobDetails.JobState); DateTimeAssert.AreEqualIncludingKind(new DateTime(1970, 1, 5, 0, 0, 0, DateTimeKind.Utc), jobDetails.NextTriggerFireTimeUtc); Assert.AreEqual(new TimeSpan(0, 2, 0), jobDetails.NextTriggerMisfireThreshold); Assert.IsNull(jobDetails.JobSpec.JobData); Assert.IsNull(jobDetails.LastJobExecutionDetails); }
public void DelegateToGenericMock() { MockRepository mocks = new MockRepository(); IEMailFormatter <string> formatterMock = mocks.StrictMock <IEMailFormatter <string> >(); SmtpEMailSenderBase <string> senderMock = (SmtpEMailSenderBase <string>)mocks.StrictMock(typeof(SmtpEMailSenderBase <string>)); senderMock.SetFormatter(formatterMock); LastCall.Do((Action <IEMailFormatter <string> >) delegate(IEMailFormatter <string> formatter) { Assert.NotNull(formatter); }); mocks.ReplayAll(); senderMock.SetFormatter(formatterMock); mocks.VerifyAll(); }
public void IgnoreArgumentsAfterDo() { MockRepository mocks = new MockRepository(); IDemo demo = mocks.DynamicMock <IDemo>(); bool didDo = false; demo.VoidNoArgs(); LastCall .Do(SetToTrue(out didDo)) .IgnoreArguments(); mocks.ReplayAll(); demo.VoidNoArgs(); Assert.True(didDo, "Do has not been executed!"); mocks.VerifyAll(); }
public void ScheduleCompletedJob_WithStopAction(bool lastExecutionDetailsNotNull, bool lastExecutionSucceeded, bool lastEndTimeNotNull) { JobDetails jobDetails = new JobDetails(dummyJobSpec, DateTime.UtcNow); jobDetails.JobState = JobState.Completed; if (lastExecutionDetailsNotNull) { jobDetails.LastJobExecutionDetails = new JobExecutionDetails(scheduler.Guid, DateTime.UtcNow); jobDetails.LastJobExecutionDetails.Succeeded = lastExecutionSucceeded; if (lastEndTimeNotNull) { jobDetails.LastJobExecutionDetails.EndTimeUtc = DateTime.UtcNow; } } PrepareMockJobWatcher(jobDetails); Expect.Call(mockTrigger.Schedule(TriggerScheduleCondition.Latch, DateTime.UtcNow, null)) .Constraints(Rhino.Mocks.Constraints.Is.Equal(TriggerScheduleCondition.Latch), Rhino.Mocks.Constraints.Is.Anything(), Property.Value("Succeeded", lastExecutionSucceeded)) .Return(TriggerScheduleAction.Stop); Expect.Call(mockTrigger.NextFireTimeUtc).Return(null); Expect.Call(mockTrigger.NextMisfireThreshold).Return(null); mockJobStore.SaveJobDetails(jobDetails); LastCall.Do((SaveJobDetailsDelegate)WakeOnSaveJobDetails); Mocks.ReplayAll(); RunSchedulerUntilWake(); Assert.AreEqual(JobState.Stopped, jobDetails.JobState); Assert.IsNull(jobDetails.NextTriggerFireTimeUtc); Assert.IsNull(jobDetails.NextTriggerMisfireThreshold); Assert.IsNull(jobDetails.JobSpec.JobData); Assert.IsNotNull(jobDetails.LastJobExecutionDetails); Assert.AreEqual(scheduler.Guid, jobDetails.LastJobExecutionDetails.SchedulerGuid); Assert.AreEqual(lastExecutionSucceeded, jobDetails.LastJobExecutionDetails.Succeeded); Assert.IsNotNull(jobDetails.LastJobExecutionDetails.EndTimeUtc); }
[TestCase((JobState)9999)] // invalid job state public void SchedulerHandlesUnexpectedJobStateReceivedFromWatcherByStoppingTheTrigger(JobState jobState) { JobDetails jobDetails = new JobDetails(dummyJobSpec, DateTime.UtcNow); jobDetails.JobState = jobState; PrepareMockJobWatcher(jobDetails); mockJobStore.SaveJobDetails(jobDetails); LastCall.Do((SaveJobDetailsDelegate)WakeOnSaveJobDetails); Mocks.ReplayAll(); RunSchedulerUntilWake(); Assert.AreEqual(JobState.Stopped, jobDetails.JobState); Assert.IsNull(jobDetails.NextTriggerFireTimeUtc); Assert.IsNull(jobDetails.NextTriggerMisfireThreshold); Assert.IsNull(jobDetails.LastJobExecutionDetails); Assert.IsNull(jobDetails.JobSpec.JobData); }
public void RaiseEvent() { IWithEvents eventHolder = (IWithEvents)mocks.StrictMock(typeof(IWithEvents)); eventHolder.Blah += null; LastCall.IgnoreArguments(); raiser = LastCall.GetEventRaiser(); eventHolder.RaiseEvent(); LastCall.Do(new System.Threading.ThreadStart(UseEventRaiser)); IEventSubscriber eventSubscriber = (IEventSubscriber)mocks.StrictMock(typeof(IEventSubscriber)); eventSubscriber.Hanlder(this, EventArgs.Empty); mocks.ReplayAll(); eventHolder.Blah += new EventHandler(eventSubscriber.Hanlder); eventHolder.RaiseEvent(); mocks.VerifyAll(); }