Skip to content

Commit

Permalink
Binary data api updates (#13176)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshLove-msft authored Jul 9, 2020
1 parent 457c3af commit f99dbe8
Show file tree
Hide file tree
Showing 12 changed files with 199 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,27 @@ public readonly partial struct BinaryData
{
private readonly object _dummy;
private readonly int _dummyPrimitive;
public BinaryData(System.ReadOnlyMemory<byte> data) { throw null; }
public BinaryData(System.ReadOnlySpan<byte> data) { throw null; }
public BinaryData(string data) { throw null; }
public BinaryData(string data, System.Text.Encoding encoding) { throw null; }
public System.ReadOnlyMemory<byte> AsBytes() { throw null; }
public System.ReadOnlyMemory<byte> Bytes { get { throw null; } }
public System.Threading.Tasks.ValueTask<T> DeserializeAsync<T>(Azure.Core.ObjectSerializer serializer, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public System.Threading.Tasks.ValueTask<T> DeserializeAsync<T>(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public T Deserialize<T>(Azure.Core.ObjectSerializer serializer, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public T Deserialize<T>(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
public override bool Equals(object? obj) { throw null; }
public static System.Threading.Tasks.Task<Azure.Core.BinaryData> FromSerializableAsync<T>(T data, Azure.Core.ObjectSerializer serializer, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public static Azure.Core.BinaryData FromSerializable<T>(T data, Azure.Core.ObjectSerializer serializer, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public static Azure.Core.BinaryData FromMemory(System.ReadOnlyMemory<byte> data) { throw null; }
public static Azure.Core.BinaryData FromStream(System.IO.Stream stream) { throw null; }
public static System.Threading.Tasks.Task<Azure.Core.BinaryData> FromStreamAsync(System.IO.Stream stream, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
public override int GetHashCode() { throw null; }
public static implicit operator System.ReadOnlyMemory<byte> (Azure.Core.BinaryData data) { throw null; }
public static System.Threading.Tasks.Task<Azure.Core.BinaryData> SerializeAsync<T>(T data, Azure.Core.ObjectSerializer serializer, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public static System.Threading.Tasks.Task<Azure.Core.BinaryData> SerializeAsync<T>(T data, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public static Azure.Core.BinaryData Serialize<T>(T data, Azure.Core.ObjectSerializer serializer, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public static Azure.Core.BinaryData Serialize<T>(T data, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public System.IO.Stream ToStream() { throw null; }
public override string ToString() { throw null; }
public string ToString(System.Text.Encoding encoding) { throw null; }
}
public partial class DynamicJson : System.Dynamic.IDynamicMetaObjectProvider
{
Expand Down
191 changes: 125 additions & 66 deletions sdk/core/Azure.Core.Experimental/src/Primitives/BinaryData.cs

Large diffs are not rendered by default.

83 changes: 51 additions & 32 deletions sdk/core/Azure.Core.Experimental/tests/BinaryDataTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Azure.Core;
using Moq;
using NUnit.Framework;

Expand All @@ -17,31 +17,40 @@ public class BinaryDataTests
[Test]
public void CanCreateBinaryDataFromBytes()
{
var payload = Encoding.UTF8.GetBytes("some data");
var data = new BinaryData(payload);
Assert.AreEqual(payload, data.AsBytes().ToArray());
byte[] payload = Encoding.UTF8.GetBytes("some data");
BinaryData data = BinaryData.FromMemory(payload);
Assert.AreEqual(payload, data.Bytes.ToArray());

MemoryMarshal.TryGetArray<byte>(payload, out var array);
Assert.AreSame(payload, array.Array);

// using implicit conversion
ReadOnlyMemory<byte> bytes = data;
Assert.AreEqual(payload, bytes.ToArray());
}

[Test]
public void CanCreateBinaryDataFromString()
public void CanCreateBinaryDataUsingSpanCtor()
{
var payload = "some data";
var data = new BinaryData(payload);
Assert.AreEqual(payload, data.ToString());
byte[] payload = Encoding.UTF8.GetBytes("some data");
BinaryData data = new BinaryData(payload);

Assert.AreNotSame(payload, data.Bytes);
Assert.AreNotEqual(payload, data.Bytes);

Assert.AreEqual(payload, data.Bytes.ToArray());

// using implicit conversion
ReadOnlyMemory<byte> bytes = data;
Assert.AreEqual(payload, bytes.ToArray());
}

[Test]
public void ToStringThrowsOnNullEncoding()
public void CanCreateBinaryDataFromString()
{
var payload = "some data";
var data = new BinaryData(payload);
Assert.That(
() => data.ToString(null),
Throws.InstanceOf<ArgumentException>());
Assert.AreEqual(payload, data.ToString());
}

[Test]
Expand All @@ -50,7 +59,7 @@ public void ToStringRespectsArraySegmentBoundaries()
var payload = "pre payload post";
var bytes = Encoding.UTF8.GetBytes(payload);
var segment = new ArraySegment<byte>(bytes, 4, 7);
var data = new BinaryData(segment);
var data = BinaryData.FromMemory(segment);
Assert.AreEqual("payload", data.ToString());
}

Expand All @@ -60,7 +69,7 @@ public async Task ToStreamRespectsArraySegmentBoundaries()
var payload = "pre payload post";
var bytes = Encoding.UTF8.GetBytes(payload);
var segment = new ArraySegment<byte>(bytes, 4, 7);
var data = new BinaryData(segment);
var data = BinaryData.FromMemory(segment);
var stream = data.ToStream();
var sr = new StreamReader(stream);
Assert.AreEqual("payload", await sr.ReadToEndAsync());
Expand All @@ -73,12 +82,12 @@ public async Task CanCreateBinaryDataFromStream()
var buffer = Encoding.UTF8.GetBytes("some data");
var payload = new MemoryStream(buffer);
var data = BinaryData.FromStream(payload);
Assert.AreEqual(buffer, data.AsBytes().ToArray());
Assert.AreEqual(buffer, data.Bytes.ToArray());
Assert.AreEqual(payload, data.ToStream());

payload.Position = 0;
data = await BinaryData.FromStreamAsync(payload);
Assert.AreEqual(buffer, data.AsBytes().ToArray());
Assert.AreEqual(buffer, data.Bytes.ToArray());
Assert.AreEqual(payload, data.ToStream());
}

Expand All @@ -98,8 +107,12 @@ public async Task CanCreateBinaryDataFromCustomType()
{
var payload = new TestModel { A = "value", B = 5, C = true};
var serializer = new JsonObjectSerializer();
await AssertData(BinaryData.FromSerializable(payload, serializer));
await AssertData(await BinaryData.FromSerializableAsync(payload, serializer));

await AssertData(BinaryData.Serialize(payload, serializer));
await AssertData(await BinaryData.SerializeAsync(payload, serializer));

await AssertData(BinaryData.Serialize(payload));
await AssertData(await BinaryData.SerializeAsync(payload));

async Task AssertData(BinaryData data)
{
Expand All @@ -109,6 +122,13 @@ async Task AssertData(BinaryData data)
Assert.AreEqual(payload.A, (await data.DeserializeAsync<TestModel>(serializer)).A);
Assert.AreEqual(payload.B, (await data.DeserializeAsync<TestModel>(serializer)).B);
Assert.AreEqual(payload.C, (await data.DeserializeAsync<TestModel>(serializer)).C);

Assert.AreEqual(payload.A, data.Deserialize<TestModel>().A);
Assert.AreEqual(payload.B, data.Deserialize<TestModel>().B);
Assert.AreEqual(payload.C, data.Deserialize<TestModel>().C);
Assert.AreEqual(payload.A, (await data.DeserializeAsync<TestModel>()).A);
Assert.AreEqual(payload.B, (await data.DeserializeAsync<TestModel>()).B);
Assert.AreEqual(payload.C, (await data.DeserializeAsync<TestModel>()).C);
}
}

Expand All @@ -117,10 +137,10 @@ public void FromSerializableThrowsOnNullSerializer()
{
var payload = new TestModel { A = "value", B = 5, C = true };
Assert.That(
() => BinaryData.FromSerializable(payload, null),
() => BinaryData.Serialize(payload, null),
Throws.InstanceOf<ArgumentNullException>());
Assert.That(
async () => await BinaryData.FromSerializableAsync(payload, null),
async () => await BinaryData.SerializeAsync(payload, null),
Throws.InstanceOf<ArgumentNullException>());
}

Expand All @@ -140,17 +160,16 @@ public void CreateThrowsOnNullStream()
public async Task DeserializeThrowsExceptionOnIncompatibleType()
{
var payload = new TestModel { A = "value", B = 5, C = true };
var serializer = new JsonObjectSerializer();
AssertData(BinaryData.FromSerializable(payload, serializer));
AssertData(await BinaryData.FromSerializableAsync(payload, serializer));
AssertData(BinaryData.Serialize(payload));
AssertData(await BinaryData.SerializeAsync(payload));

void AssertData(BinaryData data)
{
Assert.That(
() => data.Deserialize<string>(serializer),
() => data.Deserialize<string>(),
Throws.InstanceOf<Exception>());
Assert.That(
async () => await data.DeserializeAsync<string>(serializer),
async () => await data.DeserializeAsync<string>(),
Throws.InstanceOf<Exception>());
}
}
Expand All @@ -160,7 +179,7 @@ public void DeserializeThrowsOnNullSerializer()
{
var payload = new TestModel { A = "value", B = 5, C = true };
var serializer = new JsonObjectSerializer();
var data = BinaryData.FromSerializable(payload, serializer);
var data = BinaryData.Serialize(payload, serializer);
Assert.That(
() => data.Deserialize<TestModel>(null),
Throws.InstanceOf<ArgumentNullException>());
Expand All @@ -173,11 +192,11 @@ public void DeserializeThrowsOnNullSerializer()
public void EqualsRespectsReferenceEquality()
{
var payload = Encoding.UTF8.GetBytes("some data");
var a = new BinaryData(payload);
var b = new BinaryData(payload);
var a = BinaryData.FromMemory(payload);
var b = BinaryData.FromMemory(payload);
Assert.AreEqual(a, b);

var c = new BinaryData(Encoding.UTF8.GetBytes("some data"));
var c = BinaryData.FromMemory(Encoding.UTF8.GetBytes("some data"));
Assert.AreNotEqual(a, c);

Assert.AreNotEqual(a, "string data");
Expand All @@ -187,16 +206,16 @@ public void EqualsRespectsReferenceEquality()
public void GetHashCodeWorks()
{
var payload = Encoding.UTF8.GetBytes("some data");
var a = new BinaryData(payload);
var b = new BinaryData(payload);
var a = BinaryData.FromMemory(payload);
var b = BinaryData.FromMemory(payload);
var set = new HashSet<BinaryData>
{
a
};
// hashcodes of a and b should match since instances use same memory.
Assert.IsTrue(set.Contains(b));

var c = new BinaryData(Encoding.UTF8.GetBytes("some data"));
var c = BinaryData.FromMemory(Encoding.UTF8.GetBytes("some data"));
// c should have a different hash code
Assert.IsFalse(set.Contains(c));
set.Add(c);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ public ServiceBusMessage(Azure.Core.BinaryData body) { }
public ServiceBusMessage(Azure.Messaging.ServiceBus.ServiceBusReceivedMessage receivedMessage) { }
public ServiceBusMessage(System.ReadOnlyMemory<byte> body) { }
public ServiceBusMessage(string body) { }
public ServiceBusMessage(string body, System.Text.Encoding encoding) { }
public Azure.Core.BinaryData Body { get { throw null; } set { } }
public string ContentType { get { throw null; } set { } }
public string CorrelationId { get { throw null; } set { } }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ private static ArraySegment<byte> ReadStreamToArraySegment(Stream stream)

public static AmqpMessage SBMessageToAmqpMessage(SBMessage sbMessage)
{
ReadOnlyMemory<byte> bodyBytes = sbMessage.Body.AsBytes();
ReadOnlyMemory<byte> bodyBytes = sbMessage.Body;
var body = new ArraySegment<byte>((bodyBytes.IsEmpty) ? Array.Empty<byte>() : bodyBytes.ToArray());
var amqpMessage = AmqpMessage.Create(new Data { Value = body });
amqpMessage.Properties.MessageId = sbMessage.MessageId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Azure.Core.Experimental" />
<PackageReference Include="Azure.Identity" />
<PackageReference Include="Microsoft.Azure.Amqp" />
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" />
Expand All @@ -33,6 +32,9 @@
<Compile Include="$(AzureCoreSharedSources)PageResponseEnumerator.cs" Link="SharedSource\Azure.Core\PageResponseEnumerator.cs" />

</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\core\Azure.Core.Experimental\src\Azure.Core.Experimental.csproj" />
</ItemGroup>
<ItemGroup>
<Compile Update="Resources.Designer.cs">
<DesignTime>True</DesignTime>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

using System.Collections.Generic;
using System.Threading.Tasks;
using Azure.Core.Pipeline;

namespace Azure.Messaging.ServiceBus.Diagnostics
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,9 @@ public ServiceBusMessage()
/// Creates a new message from the specified string, using UTF-8 encoding.
/// </summary>
/// <param name="body">The payload of the message as a string.</param>
public ServiceBusMessage(string body) :
this(body, Encoding.UTF8)
public ServiceBusMessage(string body)
{
}

/// <summary>
/// Creates a new message from the specified string, using the specified encoding.
/// </summary>
/// <param name="body">The payload of the message as a string.</param>
/// <param name="encoding">The encoding to use for the body.</param>
public ServiceBusMessage(string body, Encoding encoding)
{
Argument.AssertNotNull(encoding, nameof(encoding));
Body = new BinaryData(body, encoding);
Body = new BinaryData(body);
Properties = new Dictionary<string, object>();
}

Expand All @@ -59,7 +48,7 @@ public ServiceBusMessage(string body, Encoding encoding)
/// <param name="body">The payload of the message in bytes.</param>
public ServiceBusMessage(ReadOnlyMemory<byte> body)
{
Body = new BinaryData(body);
Body = BinaryData.FromMemory(body);
Properties = new Dictionary<string, object>();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void ConvertSBMessageToAmqpMessageAndBack()
var convertedSbMessage = AmqpMessageConverter.AmqpMessageToSBMessage(amqpMessage);

Assert.AreEqual("SomeUserProperty", convertedSbMessage.Properties["UserProperty"]);
Assert.AreEqual(messageBody, convertedSbMessage.Body.AsBytes().ToArray());
Assert.AreEqual(messageBody, convertedSbMessage.Body.Bytes.ToArray());
Assert.AreEqual(messageId, convertedSbMessage.MessageId);
Assert.AreEqual(partitionKey, convertedSbMessage.PartitionKey);
Assert.AreEqual(viaPartitionKey, convertedSbMessage.ViaPartitionKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public async Task CanSendMessageWithMaxSize()
var receiver = client.CreateReceiver(scope.QueueName);
var receivedMaxSizeMessage = await receiver.ReceiveMessageAsync();
await receiver.CompleteMessageAsync(receivedMaxSizeMessage.LockToken);
Assert.AreEqual(maxPayload, receivedMaxSizeMessage.Body.AsBytes().ToArray());
Assert.AreEqual(maxPayload, receivedMaxSizeMessage.Body.Bytes.ToArray());
}
}

Expand Down Expand Up @@ -124,7 +124,7 @@ public async Task CreateFromReceivedMessageCopiesProperties()

void AssertMessagesEqual(ServiceBusMessage sentMessage, ServiceBusReceivedMessage received)
{
Assert.IsTrue(received.Body.AsBytes().ToArray().SequenceEqual(sentMessage.Body.AsBytes().ToArray()));
Assert.IsTrue(received.Body.Bytes.ToArray().SequenceEqual(sentMessage.Body.Bytes.ToArray()));
Assert.AreEqual(received.ContentType, sentMessage.ContentType);
Assert.AreEqual(received.CorrelationId, sentMessage.CorrelationId);
Assert.AreEqual(received.Label, sentMessage.Label);
Expand Down Expand Up @@ -158,7 +158,7 @@ public async Task SendJsonBodyMessage()
B = 5,
C = false
};
var body = BinaryData.FromSerializable(testBody, serializer);
var body = BinaryData.Serialize(testBody, serializer);
var msg = new ServiceBusMessage(body);

await sender.SendMessageAsync(msg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@ public void SetMessageBodyToString()
var messageBody = "some message";
var message = new ServiceBusMessage(messageBody);
Assert.AreEqual(message.Body.ToString(), messageBody);
Assert.AreEqual(message.Body.ToString(), messageBody);

message = new ServiceBusMessage(messageBody, Encoding.UTF32);
Assert.AreEqual(message.Body.ToString(Encoding.UTF32), messageBody);
Assert.AreNotEqual(message.Body.ToString(), messageBody);
}
}
}
Loading

0 comments on commit f99dbe8

Please sign in to comment.