Ejemplo n.º 1
0
        public void TestNotifications(bool shouldBatch, int toQueue, int expectSuccessful, int expectFailed, int[] indexesToFail = null)
        {
            testPort++;

            int msgIdOn = 1000;

            int pushFailCount = 0;
            int pushSuccessCount = 0;

            int serverReceivedCount = 0;
            int serverReceivedFailCount = 0;
            int serverReceivedSuccessCount = 0;

            //var notification = new GcmNotification();

            var server = new TestServers.GcmTestServer();

            server.MessageResponseFilters.Add(new GcmMessageResponseFilter()
            {
                IsMatch = (request, s) => {
                    return s.Equals("FAIL", StringComparison.InvariantCultureIgnoreCase);
                },
                Status = new GcmMessageResult() {
                    ResponseStatus = GcmMessageTransportResponseStatus.InvalidRegistration,
                    MessageId = "1:" + msgIdOn++
                }
            });

            server.MessageResponseFilters.Add(new GcmMessageResponseFilter()
                                              {
                IsMatch = (request, s) => {
                    return s.Equals("NOTREGISTERED", StringComparison.InvariantCultureIgnoreCase);
                },
                Status = new GcmMessageResult() {
                    ResponseStatus = GcmMessageTransportResponseStatus.NotRegistered,
                    MessageId = "1:" + msgIdOn++
                }
            });
            //var waitServerFinished = new ManualResetEvent(false);

            server.Start(testPort, response =>
                        {
                            serverReceivedCount += (int)response.NumberOfCanonicalIds;
                            serverReceivedSuccessCount += (int) response.NumberOfSuccesses;
                            serverReceivedFailCount += (int) response.NumberOfFailures;
                        });

            var settings = new GcmPushChannelSettings("SENDERAUTHTOKEN");
            settings.OverrideUrl("http://*****:*****@"{""key"":""value1""}";

            if (shouldBatch)
            {
                var regIds = new List<string>();

                for (int i = 0; i < toQueue; i++)
                    regIds.Add((indexesToFail != null && indexesToFail.Contains(i)) ? "FAIL" : "SUCCESS");

                var n = new GcmNotification();
                n.RegistrationIds.AddRange(regIds);
                n.WithJson(json);

                push.QueueNotification(n);
            }
            else
            {
                for (int i = 0; i < toQueue; i++)
                    push.QueueNotification(new GcmNotification()
                        .ForDeviceRegistrationId((indexesToFail != null && indexesToFail.Contains(i)) ? "FAIL" : "SUCCESS")
                        .WithJson(json));
            }

            push.Stop();
            push.Dispose();

            server.Dispose();
            //waitServerFinished.WaitOne();

            Console.WriteLine("TEST-> DISPOSE.");

            Assert.AreEqual(toQueue, serverReceivedCount, "Server - Received Count");
            Assert.AreEqual(expectFailed, serverReceivedFailCount, "Server - Failed Count");
            Assert.AreEqual(expectSuccessful, serverReceivedSuccessCount, "Server - Success Count");

            Assert.AreEqual(expectFailed, pushFailCount, "Client - Failed Count");
            Assert.AreEqual(expectSuccessful, pushSuccessCount, "Client - Success Count");
        }
Ejemplo n.º 2
0
        public void TestNotifications(List<GcmNotification> notifications,
		                              List<GcmMessageResponseFilter> responseFilters,
		                              Action<object, INotification> sentCallback,
		                              Action<object, INotification, Exception> failedCallback,
		                              Action<object, string, string, INotification> subscriptionChangedCallback,
		                              Action<object, string, DateTime, INotification> subscriptionExpiredCallback)
        {
            testPort++;

            int pushFailCount = 0;
            int pushSuccessCount = 0;

            int serverReceivedCount = 0;
            int serverReceivedFailCount = 0;
            int serverReceivedSuccessCount = 0;

            var server = new TestServers.GcmTestServer();

            server.MessageResponseFilters.AddRange(responseFilters);

            server.Start(testPort, response => {
                serverReceivedCount += (int)response.NumberOfCanonicalIds;
                serverReceivedSuccessCount += (int) response.NumberOfSuccesses;
                serverReceivedFailCount += (int) response.NumberOfFailures;
            });

            var settings = new GcmPushChannelSettings("SENDERAUTHTOKEN");
            settings.OverrideUrl("http://localhost:" + (testPort) + "/");

            var push = new GcmPushService(settings);
            push.OnNotificationSent += (sender, notification1) => {
                pushSuccessCount++;
                sentCallback(sender, notification1);
            };
            push.OnNotificationFailed += (sender, notification1, error) => {
                pushFailCount++;
                failedCallback(sender, notification1, error);
            };
            push.OnDeviceSubscriptionChanged += (sender, oldSubscriptionId, newSubscriptionId, notification) => subscriptionChangedCallback(sender, oldSubscriptionId, newSubscriptionId, notification);
            push.OnDeviceSubscriptionExpired += (sender, expiredSubscriptionId, expirationDateUtc, notification) => subscriptionExpiredCallback(sender, expiredSubscriptionId, expirationDateUtc, notification);

            foreach (var n in notifications)
                push.QueueNotification(n);

            push.Stop();
            push.Dispose();

            server.Dispose();
            //waitServerFinished.WaitOne();

            Console.WriteLine("TEST-> DISPOSE.");
        }
Ejemplo n.º 3
0
		public void TestNotifications(bool shouldBatch, int toQueue, int expectSuccessful, int expectFailed, int[] indexesToFail = null, bool waitForScaling = false)
		{
			testPort++;

			int msgIdOn = 1000;

			int pushFailCount = 0;
			int pushSuccessCount = 0;

			int serverReceivedCount = 0;
			int serverReceivedFailCount = 0;
			int serverReceivedSuccessCount = 0;

			//var notification = new GcmNotification();

			var server = new TestServers.GcmTestServer();

			server.MessageResponseFilters.Add(new GcmMessageResponseFilter()
			{
				IsMatch = (request, s) => {
					return s.Equals("FAIL", StringComparison.InvariantCultureIgnoreCase);
				},
				Status = new GcmMessageResult() { 
					ResponseStatus = GcmMessageTransportResponseStatus.InvalidRegistration,
					MessageId = "1:" + msgIdOn++
				}
			});

			server.MessageResponseFilters.Add(new GcmMessageResponseFilter()
			                                  {
				IsMatch = (request, s) => {
					return s.Equals("NOTREGISTERED", StringComparison.InvariantCultureIgnoreCase);
				},
				Status = new GcmMessageResult() { 
					ResponseStatus = GcmMessageTransportResponseStatus.NotRegistered,
					MessageId = "1:" + msgIdOn++
				}
			});
			//var waitServerFinished = new ManualResetEvent(false);
			
			server.Start(testPort, response =>
						{
							serverReceivedCount += (int)response.NumberOfCanonicalIds;
							serverReceivedSuccessCount += (int) response.NumberOfSuccesses;
							serverReceivedFailCount += (int) response.NumberOfFailures;
						});

			

			var settings = new GcmPushChannelSettings("SENDERAUTHTOKEN");
			settings.OverrideUrl("http://*****:*****@"{""key"":""value1""}";

			if (shouldBatch)
			{
				var regIds = new List<string>();

				for (int i = 0; i < toQueue; i++)
					regIds.Add((indexesToFail != null && indexesToFail.Contains(i)) ? "FAIL" : "SUCCESS");

				var n = new GcmNotification();
				n.RegistrationIds.AddRange(regIds);
				n.WithJson(json);
				
				push.QueueNotification(n);
			}
			else
			{
				for (int i = 0; i < toQueue; i++)
					push.QueueNotification(new GcmNotification()
						.ForDeviceRegistrationId((indexesToFail != null && indexesToFail.Contains(i)) ? "FAIL" : "SUCCESS")
						.WithJson(json));
			}

			

			

            Console.WriteLine("Avg Queue Wait Time: " + push.AverageQueueWaitTime + " ms");
            Console.WriteLine("Avg Send Time: " + push.AverageSendTime + " ms");

            if (waitForScaling)
            {
               while (push.QueueLength > 0)
                   Thread.Sleep(500);

                Console.WriteLine("Sleeping 3 minutes for autoscaling...");
                Thread.Sleep(TimeSpan.FromMinutes(3));

                Console.WriteLine("Channel Count: " + push.ChannelCount);
                Assert.IsTrue(push.ChannelCount <= 1);
            }

            push.Stop();
			push.Dispose();

			server.Dispose();
			//waitServerFinished.WaitOne();

			Console.WriteLine("TEST-> DISPOSE.");

			Assert.AreEqual(toQueue, serverReceivedCount, "Server - Received Count");
			Assert.AreEqual(expectFailed, serverReceivedFailCount, "Server - Failed Count");
			Assert.AreEqual(expectSuccessful, serverReceivedSuccessCount, "Server - Success Count");

			Assert.AreEqual(expectFailed, pushFailCount, "Client - Failed Count");
			Assert.AreEqual(expectSuccessful, pushSuccessCount, "Client - Success Count");
		}