Skip to content

Commit 6e80cc5

Browse files
committed
Moved MemoryStream to an instance variable
1 parent f9136f9 commit 6e80cc5

File tree

3 files changed

+67
-35
lines changed

3 files changed

+67
-35
lines changed

src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft/Data/SqlClient/UdtSerialization/InvalidSerializationTest.cs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,31 @@ namespace Microsoft.Data.SqlClient.UnitTests.UdtSerialization;
1414
/// <summary>
1515
/// Attempts to serialize types which do not meet the requirements for either user-defined or native serialization.
1616
/// </summary>
17-
public class InvalidSerializationTest
17+
public sealed class InvalidSerializationTest : IDisposable
1818
{
19+
private readonly MemoryStream _stream;
20+
21+
/// <summary>
22+
/// Initializes the MemoryStream used for all tests in this class.
23+
/// </summary>
24+
public InvalidSerializationTest()
25+
{
26+
_stream = new MemoryStream();
27+
}
28+
29+
void IDisposable.Dispose()
30+
{
31+
_stream.Dispose();
32+
}
33+
1934
/// <summary>
2035
/// Attempts to serialize a class that does not have the SqlUserDefinedType attribute. Verifies that this fails.
2136
/// </summary>
2237
[Fact]
2338
public void RequiresSqlUserDefinedTypeAttribute()
2439
{
25-
using MemoryStream stream = new();
26-
2740
var exception = Assert.Throws<InvalidUdtException>(
28-
() => SerializationHelperSql9.Serialize(stream, new ClassMissingSqlUserDefinedTypeAttribute()));
41+
() => SerializationHelperSql9.Serialize(_stream, new ClassMissingSqlUserDefinedTypeAttribute()));
2942

3043
Assert.Equal($"'{typeof(ClassMissingSqlUserDefinedTypeAttribute).FullName}' is an invalid user defined type, reason: no UDT attribute.", exception.Message);
3144
}
@@ -37,10 +50,8 @@ public void RequiresSqlUserDefinedTypeAttribute()
3750
[Fact]
3851
public void CannotSerializeUnknownFormattedType()
3952
{
40-
using MemoryStream stream = new();
41-
4253
var exception = Assert.Throws<ArgumentOutOfRangeException>("Format",
43-
() => SerializationHelperSql9.Serialize(stream, new UnknownFormattedClass()));
54+
() => SerializationHelperSql9.Serialize(_stream, new UnknownFormattedClass()));
4455

4556
#if NET
4657
Assert.Equal("The Format enumeration value, 0, is not supported by the format method. (Parameter 'Format')", exception.Message);

src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft/Data/SqlClient/UdtSerialization/NativeSerializationTest.cs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,23 @@ namespace Microsoft.Data.SqlClient.UnitTests.UdtSerialization;
1616
/// Tests the serialization method defined by MS-SSCLRT. Ensures that combinations of primitives and custom types round-trip.
1717
/// </summary>
1818
/// <seealso href="https://learn.microsoft.com/en-us/openspecs/sql_server_protocols/ms-ssclrt/77460aa9-8c2f-4449-a65e-1d649ebd77fa"/>
19-
public class NativeSerializationTest
19+
public sealed class NativeSerializationTest : IDisposable
2020
{
21+
private readonly MemoryStream _stream;
22+
23+
/// <summary>
24+
/// Initializes the MemoryStream used for all tests in this class.
25+
/// </summary>
26+
public NativeSerializationTest()
27+
{
28+
_stream = new MemoryStream();
29+
}
30+
31+
void IDisposable.Dispose()
32+
{
33+
_stream.Dispose();
34+
}
35+
2136
/// <summary>
2237
/// Provides a collection of test data representing non-null primitive type values and their corresponding
2338
/// serialized byte arrays.
@@ -206,9 +221,8 @@ public void CanSerializeTopLevelClass()
206221
Field1 = true,
207222
Field2 = new BoolWrapperStruct() { Field1 = true }
208223
};
209-
using MemoryStream stream = new();
210224

211-
SerializationHelperSql9.Serialize(stream, validWrapper);
225+
SerializationHelperSql9.Serialize(_stream, validWrapper);
212226
}
213227

214228
/// <summary>
@@ -224,9 +238,8 @@ public void CannotSerializeNestedClass()
224238
Field1 = true,
225239
Field2 = new BoolWrapperClass() { Field1 = true }
226240
};
227-
using MemoryStream stream = new();
228241

229-
var ex = Assert.Throws<Exception>(() => SerializationHelperSql9.Serialize(stream, invalidWrapper));
242+
var ex = Assert.Throws<Exception>(() => SerializationHelperSql9.Serialize(_stream, invalidWrapper));
230243
string expectedException = StringsHelper.GetString(Strings.SQL_CannotCreateNormalizer, invalidWrapper.Field2.GetType().FullName);
231244

232245
Assert.Equal(expectedException, ex.Message);
@@ -244,9 +257,8 @@ public void CannotSerializeNonPrimitiveType()
244257
Field1 = 1,
245258
Field2 = IntPtr.Zero
246259
};
247-
using MemoryStream stream = new();
248260

249-
var ex = Assert.Throws<Exception>(() => SerializationHelperSql9.Serialize(stream, invalidWrapper));
261+
var ex = Assert.Throws<Exception>(() => SerializationHelperSql9.Serialize(_stream, invalidWrapper));
250262
string expectedException = StringsHelper.GetString(Strings.SQL_CannotCreateNormalizer, invalidWrapper.Field2.GetType().FullName);
251263

252264
Assert.Equal(expectedException, ex.Message);
@@ -257,23 +269,22 @@ public void CannotSerializeNonPrimitiveType()
257269
/// </summary>
258270
/// <param name="inputValue">Object to serialize.</param>
259271
/// <param name="expectedValue">Expected serialization output.</param>
260-
private static void RoundtripType(object inputValue, byte[] expectedValue)
272+
private void RoundtripType(object inputValue, byte[] expectedValue)
261273
{
262-
using MemoryStream stream = new();
263274
int typeSize = SerializationHelperSql9.SizeInBytes(inputValue.GetType());
264275
int objectSize = SerializationHelperSql9.SizeInBytes(inputValue);
265276
int maxTypeSize = SerializationHelperSql9.GetUdtMaxLength(inputValue.GetType());
266277

267-
SerializationHelperSql9.Serialize(stream, inputValue);
268-
stream.Seek(0, SeekOrigin.Begin);
269-
object readPrimitive = SerializationHelperSql9.Deserialize(stream, inputValue.GetType());
278+
SerializationHelperSql9.Serialize(_stream, inputValue);
279+
_stream.Seek(0, SeekOrigin.Begin);
280+
object readPrimitive = SerializationHelperSql9.Deserialize(_stream, inputValue.GetType());
270281

271282
// For native formatting, the type size, the object size and the maximum object size will always be identical
272283
Assert.Equal(typeSize, objectSize);
273284
Assert.Equal(expectedValue.Length, typeSize);
274285
Assert.Equal(typeSize, maxTypeSize);
275286

276-
Assert.Equal(expectedValue, stream.ToArray());
287+
Assert.Equal(expectedValue, _stream.ToArray());
277288
Assert.Equal(inputValue, readPrimitive);
278289
}
279290
}

src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft/Data/SqlClient/UdtSerialization/UserDefinedSerializationTest.cs

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,23 @@ namespace Microsoft.Data.SqlClient.UnitTests.UdtSerialization;
1414
/// <summary>
1515
/// Tests the user-defined UDT serialization method. Verifies that custom types round-trip.
1616
/// </summary>
17-
public class UserDefinedSerializationTest
17+
public sealed class UserDefinedSerializationTest : IDisposable
1818
{
19+
private readonly MemoryStream _stream;
20+
21+
/// <summary>
22+
/// Initializes the MemoryStream used for all tests in this class.
23+
/// </summary>
24+
public UserDefinedSerializationTest()
25+
{
26+
_stream = new MemoryStream();
27+
}
28+
29+
void IDisposable.Dispose()
30+
{
31+
_stream.Dispose();
32+
}
33+
1934
/// <summary>
2035
/// Attempts to serialize and deserialize an instance of a struct with a user-defined serialization method.
2136
/// </summary>
@@ -39,13 +54,11 @@ public void CanSerializeClass()
3954
[Fact]
4055
public void RequiresPublicParameterlessConstructor()
4156
{
42-
using MemoryStream stream = new();
43-
44-
SerializationHelperSql9.Serialize(stream, new UserDefinedMissingPublicConstructor(true));
45-
stream.Seek(0, SeekOrigin.Begin);
57+
SerializationHelperSql9.Serialize(_stream, new UserDefinedMissingPublicConstructor(true));
58+
_stream.Seek(0, SeekOrigin.Begin);
4659

4760
Assert.Throws<MissingMethodException>(
48-
() => SerializationHelperSql9.Deserialize(stream, typeof(UserDefinedMissingPublicConstructor)));
61+
() => SerializationHelperSql9.Deserialize(_stream, typeof(UserDefinedMissingPublicConstructor)));
4962
}
5063

5164
/// <summary>
@@ -55,24 +68,21 @@ public void RequiresPublicParameterlessConstructor()
5568
[Fact]
5669
public void RequiresIBinarySerializeImplementation()
5770
{
58-
using MemoryStream stream = new();
59-
6071
Assert.Throws<InvalidCastException>(
61-
() => SerializationHelperSql9.Serialize(stream, new UserDefinedDoesNotImplementIBinarySerialize()));
72+
() => SerializationHelperSql9.Serialize(_stream, new UserDefinedDoesNotImplementIBinarySerialize()));
6273
}
6374

64-
private static void RoundtripType<T>(T userObject)
75+
private void RoundtripType<T>(T userObject)
6576
where T : IFormattingProgress
6677
{
67-
using MemoryStream stream = new();
6878
int typeSize = SerializationHelperSql9.SizeInBytes(userObject.GetType());
6979
int objectSize = SerializationHelperSql9.SizeInBytes(userObject);
7080
int maxTypeSize = SerializationHelperSql9.GetUdtMaxLength(userObject.GetType());
7181

72-
SerializationHelperSql9.Serialize(stream, userObject);
73-
stream.Seek(0, SeekOrigin.Begin);
74-
byte[] serializedValue = stream.ToArray();
75-
T readInstance = (T)SerializationHelperSql9.Deserialize(stream, userObject.GetType());
82+
SerializationHelperSql9.Serialize(_stream, userObject);
83+
_stream.Seek(0, SeekOrigin.Begin);
84+
byte[] serializedValue = _stream.ToArray();
85+
T readInstance = (T)SerializationHelperSql9.Deserialize(_stream, userObject.GetType());
7686

7787
// If this is a struct, it will have been copied by value and the write to WriteInvoked will have been made
7888
// to another copy of our object

0 commit comments

Comments
 (0)