Return-Path: X-Original-To: apmail-activemq-commits-archive@www.apache.org Delivered-To: apmail-activemq-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id C9AE010D64 for ; Tue, 16 Apr 2013 21:24:27 +0000 (UTC) Received: (qmail 60769 invoked by uid 500); 16 Apr 2013 21:24:27 -0000 Delivered-To: apmail-activemq-commits-archive@activemq.apache.org Received: (qmail 60700 invoked by uid 500); 16 Apr 2013 21:24:27 -0000 Mailing-List: contact commits-help@activemq.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@activemq.apache.org Delivered-To: mailing list commits@activemq.apache.org Received: (qmail 60693 invoked by uid 99); 16 Apr 2013 21:24:27 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 16 Apr 2013 21:24:27 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 16 Apr 2013 21:24:26 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 0945723889FA; Tue, 16 Apr 2013 21:24:06 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1468614 - in /activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src: main/csharp/Util/ConnectionAudit.cs test/csharp/Util/ConnectionAuditTest.cs Date: Tue, 16 Apr 2013 21:24:05 -0000 To: commits@activemq.apache.org From: tabish@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20130416212406.0945723889FA@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: tabish Date: Tue Apr 16 21:24:05 2013 New Revision: 1468614 URL: http://svn.apache.org/r1468614 Log: Add class ConnectionAudit and a unit test for it Added: activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Util/ConnectionAudit.cs (with props) activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/Util/ConnectionAuditTest.cs (with props) Added: activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Util/ConnectionAudit.cs URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Util/ConnectionAudit.cs?rev=1468614&view=auto ============================================================================== --- activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Util/ConnectionAudit.cs (added) +++ activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Util/ConnectionAudit.cs Tue Apr 16 21:24:05 2013 @@ -0,0 +1,139 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +using System; +using System.Collections.Generic; +using Apache.NMS.ActiveMQ.Commands; + +namespace Apache.NMS.ActiveMQ.Util +{ + public class ConnectionAudit + { + private readonly object mutex = new object(); + + private readonly Dictionary destinations = + new Dictionary(); + private readonly Dictionary dispatchers = + new Dictionary(); + + private bool checkForDuplicates = true; + public bool CheckForDuplicates + { + get { return this.checkForDuplicates; } + set { this.checkForDuplicates = value; } + } + + private int auditDepth = ActiveMQMessageAudit.DEFAULT_WINDOW_SIZE; + public int AuditDepth + { + get { return this.auditDepth; } + set { this.auditDepth = value; } + } + + private int auditMaximumProducerNumber = ActiveMQMessageAudit.MAXIMUM_PRODUCER_COUNT; + public int AuditMaximumProducerNumber + { + get { return this.auditMaximumProducerNumber; } + set { this.auditMaximumProducerNumber = value; } + } + + public ConnectionAudit() + { + } + + public ConnectionAudit(int auditDepth, int auditMaximumProducerNumber) + { + this.auditDepth = auditDepth; + this.auditMaximumProducerNumber = auditMaximumProducerNumber; + } + + public void RemoveDispatcher(IDispatcher dispatcher) + { + lock(this.mutex) + { + dispatchers.Remove(dispatcher); + } + } + + public bool IsDuplicate(IDispatcher dispatcher, Message message) + { + bool result = false; + + lock(this.mutex) + { + if (checkForDuplicates && message != null) + { + ActiveMQDestination destination = message.Destination; + if (destination != null) + { + if (destination.IsQueue) + { + ActiveMQMessageAudit audit = null; + if (!destinations.TryGetValue(destination, out audit)) + { + audit = new ActiveMQMessageAudit(auditDepth, auditMaximumProducerNumber); + destinations.Add(destination, audit); + } + result = audit.IsDuplicate(message.MessageId); + } + else + { + ActiveMQMessageAudit audit = null; + if (!dispatchers.TryGetValue(dispatcher, out audit)) + { + audit = new ActiveMQMessageAudit(auditDepth, auditMaximumProducerNumber); + dispatchers.Add(dispatcher, audit); + } + result = audit.IsDuplicate(message.MessageId); + } + } + } + } + return result; + } + + public void RollbackDuplicate(IDispatcher dispatcher, Message message) + { + lock(this.mutex) + { + if (checkForDuplicates && message != null) + { + ActiveMQDestination destination = message.Destination; + if (destination != null) + { + if (destination.IsQueue) + { + ActiveMQMessageAudit audit = null; + if (destinations.TryGetValue(destination, out audit)) + { + audit.Rollback(message.MessageId); + } + } + else + { + ActiveMQMessageAudit audit = null; + if (dispatchers.TryGetValue(dispatcher, out audit)) + { + audit.Rollback(message.MessageId); + } + } + } + } + } + } + } +} + Propchange: activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Util/ConnectionAudit.cs ------------------------------------------------------------------------------ svn:eol-style = native Added: activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/Util/ConnectionAuditTest.cs URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/Util/ConnectionAuditTest.cs?rev=1468614&view=auto ============================================================================== --- activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/Util/ConnectionAuditTest.cs (added) +++ activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/Util/ConnectionAuditTest.cs Tue Apr 16 21:24:05 2013 @@ -0,0 +1,133 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System; +using System.Collections.Generic; +using Apache.NMS; +using Apache.NMS.ActiveMQ; +using Apache.NMS.ActiveMQ.Commands; +using Apache.NMS.ActiveMQ.Util; +using NUnit.Framework; + +namespace Apache.NMS.ActiveMQ.Test +{ + [TestFixture] + public class ConnectionAuditTest + { + internal class MyDispatcher : IDispatcher + { + public void Dispatch(MessageDispatch messageDispatch) + { + } + } + + [Test] + public void TestConstructor1() + { + ConnectionAudit audit = new ConnectionAudit(); + Assert.IsTrue(audit.CheckForDuplicates); + Assert.IsTrue(audit.AuditDepth == ActiveMQMessageAudit.DEFAULT_WINDOW_SIZE); + Assert.IsTrue(audit.AuditMaximumProducerNumber == ActiveMQMessageAudit.MAXIMUM_PRODUCER_COUNT); + } + + [Test] + public void TestConstructor2() + { + ConnectionAudit audit = new ConnectionAudit(100, 200); + Assert.IsTrue(audit.CheckForDuplicates); + Assert.IsTrue(audit.AuditDepth == 100); + Assert.IsTrue(audit.AuditMaximumProducerNumber == 200); + } + + [Test] + public void testIsDuplicate() + { + int count = 10000; + ConnectionAudit audit = new ConnectionAudit(); + List list = new List(); + MyDispatcher dispatcher = new MyDispatcher(); + + ProducerId pid = new ProducerId(); + pid.ConnectionId = "test"; + pid.SessionId = 0; + pid.Value = 1; + + ActiveMQDestination destination = new ActiveMQQueue("TEST.QUEUE"); + Message message = new Message(); + message.Destination = destination; + + for (int i = 0; i < count; i++) + { + MessageId id = new MessageId(); + id.ProducerId = pid; + id.ProducerSequenceId = i; + list.Add(id); + + message.MessageId = id; + Assert.IsFalse(audit.IsDuplicate(dispatcher, message)); + } + + int index = list.Count -1 -audit.AuditDepth; + for (; index < list.Count; index++) + { + MessageId id = list[index]; + message.MessageId = id; + Assert.IsTrue(audit.IsDuplicate(dispatcher, message), "duplicate msg:" + id); + } + } + + [Test] + public void testRollbackDuplicate() + { + int count = 10000; + ConnectionAudit audit = new ConnectionAudit(); + List list = new List(); + MyDispatcher dispatcher = new MyDispatcher(); + + ProducerId pid = new ProducerId(); + pid.ConnectionId = "test"; + pid.SessionId = 0; + pid.Value = 1; + + ActiveMQDestination destination = new ActiveMQQueue("TEST.QUEUE"); + Message message = new Message(); + message.Destination = destination; + + for (int i = 0; i < count; i++) + { + MessageId id = new MessageId(); + id.ProducerId = pid; + id.ProducerSequenceId = i; + list.Add(id); + + message.MessageId = id; + Assert.IsFalse(audit.IsDuplicate(dispatcher, message)); + } + + int index = list.Count -1 -audit.AuditDepth; + for (; index < list.Count; index++) + { + MessageId id = list[index]; + message.MessageId = id; + Assert.IsTrue(audit.IsDuplicate(dispatcher, message), "duplicate msg:" + id); + audit.RollbackDuplicate(dispatcher, message); + Assert.IsFalse(audit.IsDuplicate(dispatcher, message), "error: duplicate msg:" + id); + } + } + } +} + Propchange: activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/Util/ConnectionAuditTest.cs ------------------------------------------------------------------------------ svn:eol-style = native