public MessagePing Add(MessagePing data) #endif { MessagePing returnValue; int recordAdded; try { returnValue = this.MessageEntity.Add(data); recordAdded = this.SaveChanges(); } catch (Exception ex) { //TODO: Log the exception throw; } bool couldNotAddRecord = 0 >= recordAdded; if (couldNotAddRecord) { returnValue = null; } return returnValue; }
public void MessagePing(ClientSocket clientSocket, MessageBase messageBase) { clientSocket.LastPingTime = _serverSocket.GetTimeStamp(); MessagePing messagePong = new MessagePing(); _serverSocket.Send(clientSocket, messagePong); }
public static void CleanUpAfterTests() { dummyMessage = null; entInstance = null; repoInstance = null; membershipInstance = null; profileInstance = null; userInstance = null; }
/// <summary> /// 心跳包 /// </summary> /// <param name="clientSocket"></param> /// <param name="messageBase"></param> public static void MessagePing(ClientSocket clientSocket, MessageBase messageBase) { Debug.Log("收到客户端的心跳包"); // 刷新心跳时间 clientSocket.LastPingTime = ServerSocket.GetTimeStamp(); MessagePing messagePing = (MessagePing)messageBase; ServerSocket.Send(clientSocket, messagePing); }
public static void InitializeForTests(TestContext contextInstance) { testContextInstance = contextInstance; recordId = Guid.NewGuid(); dummyPing = new MessagePing() { Id = recordId, Source = DEFAULT_SOURCE, Destination = DEFAULT_DESTINATION, Message = "Hello!!", MessageSentUTC = DateTime.UtcNow }; dummyPingReply = new MessagePing() { Id = recordId, Source = DEFAULT_DESTINATION, Destination = DEFAULT_SOURCE, Message = "Vanakkam!!", MessageSentUTC = DateTime.UtcNow }; pings = new List<MessagePing>() { dummyPing, dummyPingReply }.AsQueryable<MessagePing>(); entInstance = new Mock<DbSet<MessagePing>>(); repoInstance = new Mock<MessagingContext>(new string[] { "Kalanjiyam" }); membershipInstance = new Mock<IMembershipService>(); profileInstance = new Mock<IProfileService>(); userInstance = new Mock<MembershipUser>(); repoInstance.Setup(ent => ent.FindOne(It.IsAny<object[]>())).Returns(dummyPing); repoInstance.Setup(ent => ent.Add(It.IsAny<MessagePing>())).Returns(dummyPing); userInstance.Setup(usr => usr.ProviderUserKey).Returns(recordId); membershipInstance.Setup(mem => mem.CreateUser(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>())). Returns(MembershipCreateStatus.Success); membershipInstance.Setup(mem => mem.GetUser(It.IsAny<string>())).Returns(userInstance.Object); profileInstance.Setup(pro => pro.GetPropertyValue(It.IsAny<string>(), It.IsAny<string>())).Returns("http://www.google.com"); repoInstance.Setup( ent => ent.Where( It.IsAny<Expression<Func<MessagePing, bool>>>() ) ).Returns( pings ).Callback<Expression<Func<MessagePing, bool>>>( (expr) => { passedExpression = expr; } ); entInstance.As<IQueryable<MessagePing>>().Setup(rec => rec.Provider).Returns(pings.Provider); entInstance.As<IQueryable<MessagePing>>().Setup(rec => rec.Expression).Returns(pings.Expression); entInstance.As<IQueryable<MessagePing>>().Setup(rec => rec.ElementType).Returns(pings.ElementType); entInstance.As<IQueryable<MessagePing>>().Setup(rec => rec.GetEnumerator()).Returns(pings.GetEnumerator()); }
public bool PostMessage(MessagePing messageDetails) { bool postResult = default(bool); try { postResult = postSorter.PostMessage(messageDetails); } catch (Exception ex) { //TODO: Log exception properly throw; } return postResult; }
void PingThread() { while (_socket != null && _socket.Connected) { long timeNow = GetTimeStamp(); if (timeNow - _lastPingTime > Consts.PingInterval) { MessagePing msgPing = new MessagePing(); SendMessage(msgPing); _lastPingTime = GetTimeStamp(); } //如果心跳包过长时间没收到,就关闭连接 if (timeNow - _lastPongTime > Consts.PingInterval * 4) { Close(false); } } }
/// <summary> /// 心跳线程,给服务器判断,是否维持与服务器的连接 /// </summary> void PingThread() { while (m_Socket != null && m_Socket.Connected) { long timeNow = GetTimeStamp(); if (timeNow - lastPingTime > m_PingInterval) { MessagePing messagePing = new MessagePing(); SendMessage(messagePing); lastPingTime = GetTimeStamp(); } // 如果心跳包过长时间没有收到,就关闭连接 if (timeNow - lastPongTime > m_PingInterval * 4) { Close(false); } } }
public bool PostMessage(MessagePing messageDetails) { List<ValidationResult> validationSummary; //Allowing the Id to be not available bool isNotIdPresent = null == messageDetails.Id || Guid.Empty == messageDetails.Id; if (isNotIdPresent) { messageDetails.Id = Guid.NewGuid(); } bool continueProcessingPostValidation = ValidatePostedMessage(messageDetails, out validationSummary); DateTime UtcTimeReference = DateTime.UtcNow; //Test it is not a loopback message bool isNotLoopBack = messageDetails.Source != messageDetails.Destination; //Refresh the recieve time always messageDetails.MessageRecievedUTC = UtcTimeReference; bool dateRangeValidation = continueProcessingPostValidation && isNotLoopBack && UtcTimeReference.AddDays(-7) <= messageDetails.MessageSentUTC && messageDetails.MessageSentUTC <= UtcTimeReference && messageDetails.MessageRecievedUTC > messageDetails.MessageSentUTC; bool operationResult; MessagePing savedMessage = null; if (dateRangeValidation) { try { savedMessage = Storage.Add(messageDetails); bool messageSavedSuccessfully = null != savedMessage && null != savedMessage.Id; if (messageSavedSuccessfully) { operationResult = true; } else { operationResult = false; } } catch (ValidationException) { throw; } } else { operationResult = false; } return operationResult; }
public void DoesServerPreserveSentTimeForMessage() { testSubject = new WorkerProcess(repoInstance.Object, membershipInstance.Object, profileInstance.Object); DateTime timestamp = DateTime.UtcNow; MessagePing data = new MessagePing() { Id = Guid.NewGuid(), Source = DefaultSource, Destination = DefaultDestination, Message = "Hi!!", MessageSentUTC = timestamp, MessageRecievedUTC = null }; bool returnValue = testSubject.PostMessage(data); Assert.AreEqual(data.MessageSentUTC, timestamp); }
public void IsRecievedTimeBetweenMinAndMaxDateTimeValue() { testSubject = new WorkerProcess(repoInstance.Object, membershipInstance.Object, profileInstance.Object); MessagePing data = new MessagePing() { Id = Guid.NewGuid(), Source = DefaultSource, Destination = DefaultDestination, Message = "Hi!!", MessageSentUTC = DateTime.UtcNow, MessageRecievedUTC = null }; bool returnValue = testSubject.PostMessage(data); Assert.IsTrue(DateTime.MinValue < passedToRepo.MessageRecievedUTC && DateTime.MaxValue > passedToRepo.MessageRecievedUTC); }
public virtual MessagePing Add(MessagePing data)
public MessagePing Remove(MessagePing data) #endif { bool validDataIsPassed = null != data && null != data.Id && Guid.Empty != data.Id; int operationResult; MessagePing returnValue; if (validDataIsPassed) { returnValue = this.MessageEntity.Remove(data); operationResult = this.SaveChanges(); bool dataNoRemoved = 0 >= operationResult; if (dataNoRemoved) { returnValue = null; } } else { throw new Exception("Invalid data"); } return returnValue; }
public void RecievedTimeIsAlwaysGreaterThanSentTime() { testSubject = new WorkerProcess(repoInstance.Object, membershipInstance.Object, profileInstance.Object); DateTime timestamp = DateTime.UtcNow; MessagePing data = new MessagePing() { Id = Guid.NewGuid(), Source = DefaultSource, Destination = DefaultDestination, Message = "Hi!!", MessageSentUTC = timestamp.AddMilliseconds(1), MessageRecievedUTC = timestamp }; bool returnValue = testSubject.PostMessage(data); Assert.IsFalse(returnValue); }
private MessagePing SeedRecord() { MessagePing testSubject = new MessagePing(); MessagePing operationResult = null; testSubject.Id = recordId; testSubject.Source = "+919840200524"; testSubject.Destination = "+918903442090"; testSubject.Message = "Hello!!"; testSubject.MessageSentUTC = DateTime.UtcNow; using (var context = new MessagingContext(connectionStringName)) { operationResult = context.Add(testSubject); } return operationResult; }
public void SourceAndDestinationAreNotSame() { #region Setup for test testSubject = new WorkerProcess(repoInstance.Object, membershipInstance.Object, profileInstance.Object); repoInstance.Setup(ent => ent.Add(It.IsAny<MessagePing>())).Returns(dummyMessage); MessagePing testData = new MessagePing() { Source = "+919840200524", Destination = "+919840200524", Message = "Hello!", MessageSentUTC = DateTime.UtcNow }; #endregion #region Test and Assert bool postResult = testSubject.PostMessage(testData); Assert.IsFalse(postResult); #endregion }
public static void InitializeForTests(TestContext contextInstance) { recordId = Guid.NewGuid(); testContextInstance = contextInstance; dummyMessage = new MessagePing(); #region Prepare test data testDataCollection = new List<MessagePing>(); testDataCollection.Add(new MessagePing() { Source = "+919840200524", Destination = "+919941841903", Message = "Hello, Good Morning Appa!", MessageSentUTC = DateTime.UtcNow }); testDataCollection.Add(new MessagePing() { Source = "+919941841903", Destination = "+919840200524", Message = "Good Morning Beta!", MessageSentUTC = DateTime.UtcNow.AddMinutes(2) }); testDataCollection.Add(new MessagePing() { Source = "+919840200524", Destination = "+919941841903", Message = "How are you appa", MessageSentUTC = DateTime.UtcNow.AddMinutes(1) }); testDataCollection.Add(new MessagePing() { Source = "+919941841903", Destination = "+919840200524", Message = "I am very good. Hope you are also good and a rockstar in your own way..", MessageSentUTC = DateTime.UtcNow.AddMinutes(3) }); testDataCollection.Add(new MessagePing() { Source = "+919941841903", Destination = "+919551049084", Message = "Good morning dear wife", MessageSentUTC = DateTime.UtcNow.AddMinutes(3) }); testDataCollection.Add(new MessagePing() { Source = "+919940425465", Destination = "+919840200524", Message = "Gud morning <3", MessageSentUTC = DateTime.UtcNow.AddMinutes(3) }); #endregion entInstance = new Mock<DbSet<MessagePing>>(); repoInstance = new Mock<MessagingContext>(new string[] { "Kalanjiyam" }); membershipInstance = new Mock<IMembershipService>(); profileInstance = new Mock<IProfileService>(); userInstance = new Mock<MembershipUser>(); repoInstance.Setup(ent => ent.FindOne(It.IsAny<object[]>())).Returns(dummyMessage); repoInstance.Setup(ent => ent.Where(It.IsAny<Expression<Func<MessagePing, bool>>>())).Returns<Expression<Func<MessagePing, bool>>>( (expr) => { return testDataCollection.Where(expr.Compile()).ToList<MessagePing>(); }); repoInstance.Setup(ent => ent.Add(It.IsAny<MessagePing>())).Callback<MessagePing>((data) => passedToRepo = data).Returns(dummyMessage); userInstance.Setup(usr => usr.ProviderUserKey).Returns(recordId); membershipInstance.Setup(mem => mem.CreateUser(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>())). Returns(MembershipCreateStatus.Success); membershipInstance.Setup(mem => mem.GetUser(It.IsAny<string>())).Returns(userInstance.Object); profileInstance.Setup(pro => pro.GetPropertyValue(It.IsAny<string>(), It.IsAny<string>())).Returns("http://www.google.com"); }
public void IsRecievedTimeWithinPast1Hour() { testSubject = new WorkerProcess(repoInstance.Object, membershipInstance.Object, profileInstance.Object); MessagePing data = new MessagePing() { Id = Guid.NewGuid(), Source = DefaultSource, Destination = DefaultDestination, Message = "Hi!!", MessageSentUTC = DateTime.UtcNow, MessageRecievedUTC = null }; bool returnValue = testSubject.PostMessage(data); Assert.IsTrue(DateTime.UtcNow.AddHours(Within1HourPast) < passedToRepo.MessageRecievedUTC); }
public void SentTimeIsLessThanMaxTime() { testSubject = new WorkerProcess(repoInstance.Object, membershipInstance.Object, profileInstance.Object); MessagePing data = new MessagePing() { Id = Guid.NewGuid(), Source = DefaultSource, Destination = DefaultDestination, Message = "Hi!!", MessageSentUTC = DateTime.MaxValue, MessageRecievedUTC = null }; bool returnValue = testSubject.PostMessage(data); Assert.IsFalse(returnValue); }
public void SentTimeIsExactly1WeekInPast() { testSubject = new WorkerProcess(repoInstance.Object, membershipInstance.Object, profileInstance.Object); MessagePing data = new MessagePing() { Id = Guid.NewGuid(), Source = DefaultSource, Destination = DefaultDestination, Message = "Hi!!", MessageSentUTC = DateTime.UtcNow.AddDays(Exactly1WeekPast), MessageRecievedUTC = null }; bool returnValue = testSubject.PostMessage(data); Assert.IsTrue(returnValue); }
private bool ValidatePostedMessage(MessagePing data, out List<ValidationResult> validationSummary) { ICollection<ValidationResult> validationResults = new List<ValidationResult>(); bool simpleValidationOutcome = Validator.TryValidateObject(data, new ValidationContext(data), validationResults); validationSummary = validationResults as List<ValidationResult>; return simpleValidationOutcome; }
public void DoesServerAddRecievedTimeForMessage() { testSubject = new WorkerProcess(repoInstance.Object, membershipInstance.Object, profileInstance.Object); MessagePing data = new MessagePing() { Id = Guid.NewGuid(), Source = DefaultSource, Destination = DefaultDestination, Message = "Hi!!", MessageSentUTC = DateTime.UtcNow, MessageRecievedUTC = null }; bool returnValue = testSubject.PostMessage(data); Assert.IsNotNull(passedToRepo.MessageRecievedUTC); }
public void DoesMessageHaveMessageSentTime() { //TODO: Properly instantiate the WorkerProcess testSubject = new WorkerProcess(repoInstance.Object, null, null); MessagePing data = new MessagePing() { Id = Guid.NewGuid(), Source = DefaultSource, Destination = DefaultDestination, Message = "Hi!!", MessageSentUTC = DateTime.MinValue, MessageRecievedUTC = null }; bool returnValue = testSubject.PostMessage(data); Assert.IsFalse(returnValue); }
public virtual MessagePing Remove(MessagePing data)
public void IsTimeRecievedInUTCForMessage() { testSubject = new WorkerProcess(repoInstance.Object, membershipInstance.Object, profileInstance.Object); DateTime timestamp = DateTime.Now; MessagePing data = new MessagePing() { Id = Guid.NewGuid(), Source = DefaultSource, Destination = DefaultDestination, Message = "Hi!!", MessageSentUTC = timestamp, MessageRecievedUTC = null }; bool returnValue = testSubject.PostMessage(data); Assert.IsTrue(DateTimeKind.Utc == data.MessageRecievedUTC.Value.Kind); }
public static void InitializeForTests(TestContext contextInstance) { testContextInstance = contextInstance; recordId = Guid.NewGuid(); dummyMessage = new MessagePing() { Id = recordId, Source = "+919840200524", Destination = "+918903442090", Message = "Hello!!", MessageSentUTC = DateTime.UtcNow }; messagesDestination = new MessagePing() { Id = recordId, Source = "+918903442090", Destination = "+919840200524", Message = "Hello!!", MessageSentUTC = DateTime.UtcNow }; messageSearchResult = new List<MessagePing>() { dummyMessage, messagesDestination }.AsQueryable<MessagePing>(); conversationGroups = new List<MessagePing>() { messagesDestination }.AsQueryable<MessagePing>(); membershipInstance = new Mock<IMembershipService>(); profileInstance = new Mock<IProfileService>(); userInstance = new Mock<MembershipUser>(); entInstance = new Mock<DbSet<MessagePing>>(); repoInstance = new Mock<MessagingContext>(new string[] { "Kalanjiyam" }); userInstance.Setup(usr => usr.ProviderUserKey).Returns(recordId); membershipInstance.Setup(mem => mem.CreateUser(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>())). Returns(MembershipCreateStatus.Success); membershipInstance.Setup(mem => mem.GetUser(It.IsAny<string>())).Returns(userInstance.Object); profileInstance.Setup(pro => pro.GetPropertyValue(It.IsAny<string>(), It.IsAny<string>())).Returns("http://www.google.com"); repoInstance.Setup(ent => ent.FindOne(It.IsAny<object[]>())).Returns(dummyMessage); /* * NOTE: - Very Important notice to mock expressions in Moq. * The code does not do proper set up for the scenario described here * repoInstance.Setup(ent => ent.Where(d => d.Destination.Equals("+919840200524"))).Returns(conversationGroups).Verifiable(); * * Since, the call to a method in another calls with the phone number as parameter modifies the above expression to look like this - * d => (d.Destination == value(MessagingService.WorkerProcess+<>c__DisplayClass0).destination) * * Notice the string value(MessagingService.WorkerProcess+<>c_DisplayClass0).destination) since, the number is passed as parameter to the function in the class * The syntax derived here is by trial and error. The procedure to get to this needs to be determined by me. * * For now this solves the problem so I am rolling on. It took multiple sessions of 2 hrs across 3+ days to pinpoint this behavior. * * It was pinpointed by not googling extensively but by reading the error message given by Moq very cautiously. * * The code which follows 2 line down is the actual syntax that does the job. */ // //repoInstance.Setup(ent => ent.Where(It.Is<Expression<Func<MessagePing, bool>>>(expr => expr.ToString() == "d => (d.Destination == value(MessagingService.WorkerProcess+<>c__DisplayClass0).destination)"))).Returns(conversationGroups); //Particular number match //Making the mock change to the change in expression which now includes the Source and Destination check for a conversation to be listed /* * The new and changed expression looks like this - * Left Expression = {((d.Destination == value(MessagingService.WorkerProcess+<>c__DisplayClass4).destination) AndAlso (d.Source == value(MessagingService.WorkerProcess+<>c__DisplayClass4).source))} * Right Expression = {((d.Source == value(MessagingService.WorkerProcess+<>c__DisplayClass4).destination) AndAlso (d.Destination == value(MessagingService.WorkerProcess+<>c__DisplayClass4).source))} */ /*repoInstance.Setup(ent => ent.Where(It.Is<Expression<Func<MessagePing, bool>>> (expr => Expression.Lambda<Func<string>> ((MemberExpression)(((expr.Body as BinaryExpression).Right as BinaryExpression).Right as BinaryExpression).Right).Compile()() == "+919840200524") && (expr => Expression.Lambda<Func<string>> ((MemberExpression)(((expr.Body as BinaryExpression).Right as BinaryExpression).Right as BinaryExpression).Right).Compile()() == "+919840200524"))) .Returns(conversationGroups);*/ repoInstance.Setup( ent => ent.Where( It.IsAny<Expression<Func<MessagePing, bool>>>() ) ).Returns( conversationGroups ).Callback<Expression<Func<MessagePing, bool>>>( (expr) => { passedExpression = expr; } ); /*repoInstance.Setup(ent => ent.Where( It.IsAny<Expression<Func<MessagePing, bool>>>() )). Returns( new Func<Expression<Func<MessagePing, bool>>, IQueryable<MessagePing>>( expr => messageSearchResult.Where(expr.Compile()).AsQueryable() ) ).Verifiable();*/ entInstance.As<IQueryable<MessagePing>>().Setup(rec => rec.Provider).Returns(messageSearchResult.Provider); entInstance.As<IQueryable<MessagePing>>().Setup(rec => rec.Expression).Returns(messageSearchResult.Expression); entInstance.As<IQueryable<MessagePing>>().Setup(rec => rec.ElementType).Returns(messageSearchResult.ElementType); entInstance.As<IQueryable<MessagePing>>().Setup(rec => rec.GetEnumerator()).Returns(messageSearchResult.GetEnumerator()); defaultDestination = "+919840200524"; defaultSource = "+919840200524"; }
public virtual MessagePing Update(MessagePing data)
public MessagePing Update(MessagePing data) #endif { bool validDataIsPassed = null != data && !String.IsNullOrEmpty(data.Source) && !String.IsNullOrEmpty(data.Destination); if (validDataIsPassed) { } else { throw new Exception("Invalid data"); } throw new NotImplementedException(); }
public void BothSentAndRecievedTimeAreNotPresent() { testSubject = new WorkerProcess(repoInstance.Object, membershipInstance.Object, profileInstance.Object); MessagePing data = new MessagePing() { Id = Guid.NewGuid(), Source = DefaultSource, Destination = DefaultDestination, Message = "Hi!!", MessageSentUTC = null, MessageRecievedUTC = null }; bool returnValue = testSubject.PostMessage(data); Assert.IsFalse(returnValue); }