Skip to content

Commit 19483b6

Browse files
author
David R. MacIver
committed
ported the unit tests for channel allocation
1 parent e897e60 commit 19483b6

File tree

2 files changed

+142
-6
lines changed

2 files changed

+142
-6
lines changed

projects/client/RabbitMQ.Client/src/client/impl/SessionManager.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,24 +117,27 @@ public ISession Create()
117117
{
118118
throw new ChannelAllocationException();
119119
}
120-
return Create(channelNumber);
120+
return CreateInternal(channelNumber);
121121
}
122122
}
123123

124124
public ISession Create(int channelNumber)
125125
{
126-
ISession session;
127126
lock (m_sessionMap)
128127
{
129128
if (!Ints.Reserve(channelNumber))
130129
{
131130
throw new ChannelAllocationException(channelNumber);
132131
}
133-
session = new Session(m_connection, channelNumber);
134-
session.SessionShutdown += new SessionShutdownEventHandler(HandleSessionShutdown);
135-
//Console.WriteLine("SessionManager adding session "+session);
136-
m_sessionMap[channelNumber] = session;
132+
return CreateInternal(channelNumber);
137133
}
134+
}
135+
136+
public ISession CreateInternal(int channelNumber)
137+
{
138+
ISession session = new Session(m_connection, channelNumber);
139+
session.SessionShutdown += new SessionShutdownEventHandler(HandleSessionShutdown);
140+
m_sessionMap[channelNumber] = session;
138141
return session;
139142
}
140143

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
// This source code is dual-licensed under the Apache License, version
2+
// 2.0, and the Mozilla Public License, version 1.1.
3+
//
4+
// The APL v2.0:
5+
//
6+
//---------------------------------------------------------------------------
7+
// Copyright (C) 2007-2009 LShift Ltd., Cohesive Financial
8+
// Technologies LLC., and Rabbit Technologies Ltd.
9+
//
10+
// Licensed under the Apache License, Version 2.0 (the "License");
11+
// you may not use this file except in compliance with the License.
12+
// You may obtain a copy of the License at
13+
//
14+
// http://www.Apache.Org/licenses/LICENSE-2.0
15+
//
16+
// Unless required by applicable law or agreed to in writing, software
17+
// distributed under the License is distributed on an "AS IS" BASIS,
18+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19+
// See the License for the specific language governing permissions and
20+
// limitations under the License.
21+
//---------------------------------------------------------------------------
22+
//
23+
// The MPL v1.1:
24+
//
25+
//---------------------------------------------------------------------------
26+
// The contents of this file are subject to the Mozilla Public License
27+
// Version 1.1 (the "License"); you may not use this file except in
28+
// compliance with the License. You may obtain a copy of the License at
29+
// http://www.Rabbitmq.Com/mpl.Html
30+
//
31+
// Software distributed under the License is distributed on an "AS IS"
32+
// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
33+
// License for the specific language governing rights and limitations
34+
// under the License.
35+
//
36+
// The Original Code is The RabbitMQ .NET Client.
37+
//
38+
// The Initial Developers of the Original Code are LShift Ltd,
39+
// Cohesive Financial Technologies LLC, and Rabbit Technologies Ltd.
40+
//
41+
// Portions created before 22-Nov-2008 00:00:00 GMT by LShift Ltd,
42+
// Cohesive Financial Technologies LLC, or Rabbit Technologies Ltd
43+
// are Copyright (C) 2007-2008 LShift Ltd, Cohesive Financial
44+
// Technologies LLC, and Rabbit Technologies Ltd.
45+
//
46+
// Portions created by LShift Ltd are Copyright (C) 2007-2009 LShift
47+
// Ltd. Portions created by Cohesive Financial Technologies LLC are
48+
// Copyright (C) 2007-2009 Cohesive Financial Technologies
49+
// LLC. Portions created by Rabbit Technologies Ltd are Copyright
50+
// (C) 2007-2009 Rabbit Technologies Ltd.
51+
//
52+
// All Rights Reserved.
53+
//
54+
// Contributor(s): ______________________________________.
55+
//
56+
//---------------------------------------------------------------------------
57+
using NUnit.Framework;
58+
using RabbitMQ.Client;
59+
using RabbitMQ.Client.Impl;
60+
using System.Collections.Generic;
61+
62+
namespace RabbitMQ.Client.Unit
63+
{
64+
65+
[TestFixture]
66+
public class TestIModelAllocation
67+
{
68+
public const int CHANNEL_COUNT = 100;
69+
70+
IConnection C;
71+
72+
public int ModelNumber(IModel model)
73+
{
74+
return ((ModelBase)model).m_session.ChannelNumber;
75+
}
76+
77+
[SetUp] public void Connect()
78+
{
79+
C = new ConnectionFactory().CreateConnection(Protocols.FromEnvironment(), "localhost", -1);
80+
}
81+
82+
[TearDown] public void Disconnect()
83+
{
84+
C.Close();
85+
}
86+
87+
88+
[Test] public void AllocateInOrder()
89+
{
90+
for(int i = 1; i <= CHANNEL_COUNT; i++)
91+
Assert.AreEqual(i, ModelNumber(C.CreateModel()));
92+
}
93+
94+
[Test] public void AllocateAfterFreeingLast() {
95+
IModel ch = C.CreateModel();
96+
Assert.AreEqual(1, ModelNumber(ch));
97+
ch.Close();
98+
ch = C.CreateModel();
99+
Assert.AreEqual(1, ModelNumber(ch));
100+
}
101+
102+
public int CompareModels(IModel x, IModel y)
103+
{
104+
int i = ModelNumber(x);
105+
int j = ModelNumber(y);
106+
return (i < j) ? -1 : (i == j) ? 0 : 1;
107+
}
108+
109+
[Test] public void AllocateAfterFreeingMany() {
110+
List<IModel> channels = new List<IModel>();
111+
112+
for(int i = 1; i <= CHANNEL_COUNT; i++)
113+
channels.Add(C.CreateModel());
114+
115+
foreach(IModel channel in channels){
116+
channel.Close();
117+
}
118+
119+
channels = new List<IModel>();
120+
121+
for(int j = 1; j <= CHANNEL_COUNT; j++)
122+
channels.Add(C.CreateModel());
123+
124+
// In the current implementation the list should actually
125+
// already be sorted, but we don't want to force that behaviour
126+
channels.Sort(CompareModels);
127+
128+
int k = 1;
129+
foreach(IModel channel in channels)
130+
Assert.AreEqual(k++, ModelNumber(channel));
131+
}
132+
}
133+
}

0 commit comments

Comments
 (0)