Skip to content

add DeliveryModes convenience enum #1063

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions projects/RabbitMQ.Client/client/api/BasicProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public struct BasicProperties : IBasicProperties, IAmqpHeader
public string? ContentType { get; set; }
public string? ContentEncoding { get; set; }
public IDictionary<string, object?>? Headers { get; set; }
public byte DeliveryMode { get; set; }
public DeliveryModes DeliveryMode { get; set; }
public byte Priority { get; set; }
public string? CorrelationId { get; set; }
public string? ReplyTo { get; set; }
Expand All @@ -59,8 +59,15 @@ public struct BasicProperties : IBasicProperties, IAmqpHeader

public bool Persistent
{
readonly get { return DeliveryMode == 2; }
set { DeliveryMode = value ? (byte)2 : (byte)1; }
get
{
return DeliveryMode == DeliveryModes.Persistent;
}

set
{
DeliveryMode = value ? DeliveryModes.Persistent : DeliveryModes.Transient;
}
}

public PublicationAddress? ReplyToAddress
Expand Down Expand Up @@ -171,7 +178,7 @@ readonly int IAmqpWriteable.WriteTo(Span<byte> span)
if (IsDeliveryModePresent())
{
bitValue.SetBit(DeliveryModeBit);
span.GetOffset(offset++) = DeliveryMode;
span.GetOffset(offset++) = (byte)DeliveryMode;
}

if (IsPriorityPresent())
Expand Down
49 changes: 49 additions & 0 deletions projects/RabbitMQ.Client/client/api/DeliveryModes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// This source code is dual-licensed under the Apache License, version
// 2.0, and the Mozilla Public License, version 2.0.
//
// The APL v2.0:
//
//---------------------------------------------------------------------------
// Copyright (c) 2007-2020 VMware, Inc.
//
// Licensed 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
//
// https://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.
//---------------------------------------------------------------------------
//
// The MPL v2.0:
//
//---------------------------------------------------------------------------
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//
// Copyright (c) 2007-2020 VMware, Inc. All rights reserved.
//---------------------------------------------------------------------------

namespace RabbitMQ.Client
{
/// <summary>
/// Convenience enum providing compile-time names for persistent modes.
/// </summary>
public enum DeliveryModes : byte
{
/// <summary>
/// Value for transient delivery mode (not durable).
/// </summary>
Transient = 1,

/// <summary>
/// Value for persistent delivery mode (durable).
/// </summary>
Persistent = 2
}
}
4 changes: 2 additions & 2 deletions projects/RabbitMQ.Client/client/api/IBasicProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public interface IReadOnlyBasicProperties
/// <summary>
/// Non-persistent (1) or persistent (2).
/// </summary>
byte DeliveryMode { get; }
DeliveryModes DeliveryMode { get; }

/// <summary>
/// Message expiration specification.
Expand Down Expand Up @@ -236,7 +236,7 @@ public interface IBasicProperties : IReadOnlyBasicProperties
/// <summary>
/// Non-persistent (1) or persistent (2).
/// </summary>
new byte DeliveryMode { get; set; }
new DeliveryModes DeliveryMode { get; set; }

/// <summary>
/// Message expiration specification.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace RabbitMQ.Client
private readonly string? _contentType;
private readonly string? _contentEncoding;
private readonly IDictionary<string, object?>? _headers;
private readonly byte _deliveryMode;
private readonly DeliveryModes _deliveryMode;
private readonly byte _priority;
private readonly string? _correlationId;
private readonly string? _replyTo;
Expand All @@ -59,7 +59,7 @@ namespace RabbitMQ.Client
public string? ContentType => _contentType;
public string? ContentEncoding => _contentEncoding;
public IDictionary<string, object?>? Headers => _headers;
public byte DeliveryMode => _deliveryMode;
public DeliveryModes DeliveryMode => _deliveryMode;
public byte Priority => _priority;
public string? CorrelationId => _correlationId;
public string? ReplyTo => _replyTo;
Expand All @@ -71,7 +71,7 @@ namespace RabbitMQ.Client
public string? AppId => _appId;
public string? ClusterId => _clusterId;

public bool Persistent => DeliveryMode == 2;
public bool Persistent => DeliveryMode == DeliveryModes.Persistent;

public PublicationAddress? ReplyToAddress
{
Expand All @@ -90,7 +90,7 @@ public ReadOnlyBasicProperties(ReadOnlySpan<byte> span)
if (bits.IsBitSet(BasicProperties.ContentTypeBit)) { offset += WireFormatting.ReadShortstr(span.Slice(offset), out _contentType); }
if (bits.IsBitSet(BasicProperties.ContentEncodingBit)) { offset += WireFormatting.ReadShortstr(span.Slice(offset), out _contentEncoding); }
if (bits.IsBitSet(BasicProperties.HeaderBit)) { offset += WireFormatting.ReadDictionary(span.Slice(offset), out var tmpDirectory); _headers = tmpDirectory; }
if (bits.IsBitSet(BasicProperties.DeliveryModeBit)) { _deliveryMode = span[offset++]; }
if (bits.IsBitSet(BasicProperties.DeliveryModeBit)) { _deliveryMode = (DeliveryModes)span[offset++]; }
if (bits.IsBitSet(BasicProperties.PriorityBit)) { _priority = span[offset++]; }
if (bits.IsBitSet(BasicProperties.CorrelationIdBit)) { offset += WireFormatting.ReadShortstr(span.Slice(offset), out _correlationId); }
if (bits.IsBitSet(BasicProperties.ReplyToBit)) { offset += WireFormatting.ReadShortstr(span.Slice(offset), out _replyTo); }
Expand Down
2 changes: 1 addition & 1 deletion projects/RabbitMQ.Client/client/impl/EmptyBasicProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ int IAmqpWriteable.GetRequiredBufferSize()
public string? ContentEncoding => default;
public string? ContentType => default;
public string? CorrelationId => default;
public byte DeliveryMode => default;
public DeliveryModes DeliveryMode => default;
public string? Expiration => default;
public IDictionary<string, object?>? Headers => default;
public string? MessageId => default;
Expand Down
13 changes: 9 additions & 4 deletions projects/Unit/APIApproval.Approve.verified.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ namespace RabbitMQ.Client
public string? ContentEncoding { get; set; }
public string? ContentType { get; set; }
public string? CorrelationId { get; set; }
public byte DeliveryMode { get; set; }
public RabbitMQ.Client.DeliveryModes DeliveryMode { get; set; }
public string? Expiration { get; set; }
public System.Collections.Generic.IDictionary<string, object?>? Headers { get; set; }
public string? MessageId { get; set; }
Expand Down Expand Up @@ -248,6 +248,11 @@ namespace RabbitMQ.Client
public DefaultEndpointResolver(System.Collections.Generic.IEnumerable<RabbitMQ.Client.AmqpTcpEndpoint> tcpEndpoints) { }
public System.Collections.Generic.IEnumerable<RabbitMQ.Client.AmqpTcpEndpoint> All() { }
}
public enum DeliveryModes : byte
{
Transient = 1,
Persistent = 2,
}
public static class EndpointResolverExtensions
{
public static T SelectOne<T>(this RabbitMQ.Client.IEndpointResolver resolver, System.Func<RabbitMQ.Client.AmqpTcpEndpoint, T> selector) { }
Expand Down Expand Up @@ -333,7 +338,7 @@ namespace RabbitMQ.Client
new string? ContentEncoding { get; set; }
new string? ContentType { get; set; }
new string? CorrelationId { get; set; }
new byte DeliveryMode { get; set; }
new RabbitMQ.Client.DeliveryModes DeliveryMode { get; set; }
new string? Expiration { get; set; }
new System.Collections.Generic.IDictionary<string, object?>? Headers { get; set; }
new string? MessageId { get; set; }
Expand Down Expand Up @@ -527,7 +532,7 @@ namespace RabbitMQ.Client
string? ContentEncoding { get; }
string? ContentType { get; }
string? CorrelationId { get; }
byte DeliveryMode { get; }
RabbitMQ.Client.DeliveryModes DeliveryMode { get; }
string? Expiration { get; }
System.Collections.Generic.IDictionary<string, object?>? Headers { get; }
string? MessageId { get; }
Expand Down Expand Up @@ -610,7 +615,7 @@ namespace RabbitMQ.Client
public string? ContentEncoding { get; }
public string? ContentType { get; }
public string? CorrelationId { get; }
public byte DeliveryMode { get; }
public RabbitMQ.Client.DeliveryModes DeliveryMode { get; }
public string? Expiration { get; }
public System.Collections.Generic.IDictionary<string, object?>? Headers { get; }
public string? MessageId { get; }
Expand Down
12 changes: 6 additions & 6 deletions projects/Unit/TestBasicProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void TestPersistentPropertyChangesDeliveryMode_PersistentTrueDelivery2()
};

// Assert
Assert.Equal(2, subject.DeliveryMode);
Assert.Equal(DeliveryModes.Persistent, subject.DeliveryMode);
Assert.True(subject.Persistent);

Span<byte> span = new byte[1024];
Expand All @@ -57,13 +57,13 @@ public void TestPersistentPropertyChangesDeliveryMode_PersistentTrueDelivery2()
// Read from Stream
var propertiesFromStream = new ReadOnlyBasicProperties(span.Slice(0, offset));

Assert.Equal(2, propertiesFromStream.DeliveryMode);
Assert.Equal(DeliveryModes.Persistent, propertiesFromStream.DeliveryMode);
Assert.True(propertiesFromStream.Persistent);

// Verify Basic Properties
var basicProperties = new BasicProperties(propertiesFromStream);

Assert.Equal(2, basicProperties.DeliveryMode);
Assert.Equal(DeliveryModes.Persistent, basicProperties.DeliveryMode);
Assert.True(basicProperties.Persistent);
}

Expand All @@ -79,7 +79,7 @@ public void TestPersistentPropertyChangesDeliveryMode_PersistentFalseDelivery1()
};

// Assert
Assert.Equal(1, subject.DeliveryMode);
Assert.Equal(DeliveryModes.Transient, subject.DeliveryMode);
Assert.False(subject.Persistent);

Span<byte> span = new byte[1024];
Expand All @@ -88,13 +88,13 @@ public void TestPersistentPropertyChangesDeliveryMode_PersistentFalseDelivery1()
// Read from Stream
var propertiesFromStream = new ReadOnlyBasicProperties(span.Slice(0, offset));

Assert.Equal(1, propertiesFromStream.DeliveryMode);
Assert.Equal(DeliveryModes.Transient, propertiesFromStream.DeliveryMode);
Assert.False(propertiesFromStream.Persistent);

// Verify Basic Properties
var basicProperties = new BasicProperties(propertiesFromStream);

Assert.Equal(1, basicProperties.DeliveryMode);
Assert.Equal(DeliveryModes.Transient, basicProperties.DeliveryMode);
Assert.False(basicProperties.Persistent);
}

Expand Down
2 changes: 1 addition & 1 deletion projects/Unit/TestContentHeaderCodec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public void TestFullProperties()
ContentEncoding = "C",
ClusterId = "D",
CorrelationId = "E",
DeliveryMode = 1,
DeliveryMode = DeliveryModes.Transient,
Expiration = "F",
MessageId = "G",
Priority = 2,
Expand Down