|
| 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-2020 VMware, Inc. |
| 8 | +// |
| 9 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | +// you may not use this file except in compliance with the License. |
| 11 | +// You may obtain a copy of the License at |
| 12 | +// |
| 13 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 14 | +// |
| 15 | +// Unless required by applicable law or agreed to in writing, software |
| 16 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | +// See the License for the specific language governing permissions and |
| 19 | +// limitations under the License. |
| 20 | +//--------------------------------------------------------------------------- |
| 21 | +// |
| 22 | +// The MPL v1.1: |
| 23 | +// |
| 24 | +//--------------------------------------------------------------------------- |
| 25 | +// The contents of this file are subject to the Mozilla Public License |
| 26 | +// Version 1.1 (the "License"); you may not use this file except in |
| 27 | +// compliance with the License. You may obtain a copy of the License |
| 28 | +// at https://www.mozilla.org/MPL/ |
| 29 | +// |
| 30 | +// Software distributed under the License is distributed on an "AS IS" |
| 31 | +// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See |
| 32 | +// the License for the specific language governing rights and |
| 33 | +// limitations under the License. |
| 34 | +// |
| 35 | +// The Original Code is RabbitMQ. |
| 36 | +// |
| 37 | +// The Initial Developer of the Original Code is Pivotal Software, Inc. |
| 38 | +// Copyright (c) 2007-2020 VMware, Inc. All rights reserved. |
| 39 | +//--------------------------------------------------------------------------- |
| 40 | + |
| 41 | +using NUnit.Framework; |
| 42 | +using System; |
| 43 | +using System.Threading; |
| 44 | +using System.Threading.Tasks; |
| 45 | + |
| 46 | +namespace RabbitMQ.Client.Unit |
| 47 | +{ |
| 48 | + [TestFixture] |
| 49 | + public class TestPublishSharedModel |
| 50 | + { |
| 51 | + private const string ExchangeName = "TestPublishSharedModel_Ex"; |
| 52 | + private const string QueueName = "TestPublishSharedModel_Queue"; |
| 53 | + private const string PublishKey = "TestPublishSharedModel_RoutePub"; |
| 54 | + private const int Loops = 20; |
| 55 | + private const int Repeats = 1000; |
| 56 | + |
| 57 | + private readonly byte[] _body = new byte[2048]; |
| 58 | + |
| 59 | + private Exception _raisedException; |
| 60 | + |
| 61 | + [Test] |
| 62 | + public async Task MultiThreadPublishOnSharedModel() |
| 63 | + { |
| 64 | + // Arrange |
| 65 | + var connFactory = new ConnectionFactory |
| 66 | + { |
| 67 | + RequestedHeartbeat = TimeSpan.FromSeconds(60), |
| 68 | + AutomaticRecoveryEnabled = false |
| 69 | + }; |
| 70 | + |
| 71 | + using (var conn = connFactory.CreateConnection()) |
| 72 | + { |
| 73 | + conn.ConnectionShutdown += (_, args) => |
| 74 | + { |
| 75 | + if (args.Initiator != ShutdownInitiator.Application) |
| 76 | + { |
| 77 | + Assert.Fail("Unexpected connection shutdown!"); |
| 78 | + } |
| 79 | + }; |
| 80 | + |
| 81 | + using (var model = conn.CreateModel()) |
| 82 | + { |
| 83 | + model.ExchangeDeclare(ExchangeName, "topic", durable: false, autoDelete: true); |
| 84 | + model.QueueDeclare(QueueName, false, false, true, null); |
| 85 | + model.QueueBind(QueueName, ExchangeName, PublishKey, null); |
| 86 | + |
| 87 | + // Act |
| 88 | + var pubTask = Task.Run(() => NewFunction(model)); |
| 89 | + var pubTask2 = Task.Run(() => NewFunction(model)); |
| 90 | + |
| 91 | + await Task.WhenAll(pubTask, pubTask2); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + // Assert |
| 96 | + Assert.Null(_raisedException); |
| 97 | + |
| 98 | + void NewFunction(IModel model) |
| 99 | + { |
| 100 | + try |
| 101 | + { |
| 102 | + for (int i = 0; i < Loops; i++) |
| 103 | + { |
| 104 | + for (int j = 0; j < Repeats; j++) |
| 105 | + { |
| 106 | + model.BasicPublish(ExchangeName, PublishKey, false, null, _body); |
| 107 | + } |
| 108 | + |
| 109 | + Thread.Sleep(1); |
| 110 | + } |
| 111 | + } |
| 112 | + catch (Exception e) |
| 113 | + { |
| 114 | + _raisedException = e; |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments