Skip to content

Commit

Permalink
fixed all still bugy tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bezzad committed Nov 10, 2023
1 parent 6f5070c commit c398569
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
24 changes: 13 additions & 11 deletions src/Downloader.Test/UnitTests/StorageTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ public async Task OpenReadStreamTest()
public async Task SlowWriteTest()
{
// arrange
CreateStorage(DataLength);
var data = new byte[] { 1 };
var size = 1024;
CreateStorage(size);

// act
for (int i = 0; i < size; i++)
Expand All @@ -78,7 +78,7 @@ public async Task SlowWriteTest()
public async Task WriteAsyncLengthTest()
{
// arrange
CreateStorage(DataLength);
CreateStorage(0);
var length = DataLength / 2;

// act
Expand Down Expand Up @@ -136,14 +136,14 @@ public async Task WriteAsyncMultipleTimeTest()
public async Task WriteAsyncOutOfRangeExceptionTest()
{
// arrange
CreateStorage(DataLength);
CreateStorage(0);
var length = DataLength + 1;

// act
var writeMethod = async () => await Storage.WriteAsync(0, Data, length);

// assert
await Assert.ThrowsAnyAsync<ArgumentException>(writeMethod);
await Assert.ThrowsAnyAsync<ArgumentOutOfRangeException>(writeMethod);
}

[Fact]
Expand Down Expand Up @@ -179,7 +179,7 @@ public async Task FlushTest()
public async Task GetLengthTest()
{
// arrange
CreateStorage(DataLength);
CreateStorage(0);
var data = new byte[] { 0x0, 0x1, 0x2, 0x3, 0x4 };
await Storage.WriteAsync(0, data, 1);
Storage.Flush();
Expand All @@ -195,14 +195,15 @@ public async Task GetLengthTest()
public async Task TestStreamExpandability()
{
// arrange
CreateStorage(DataLength);
CreateStorage(0);
var data = new byte[] { 0x0, 0x1, 0x2, 0x3, 0x4 };
await Storage.WriteAsync(0, data, data.Length);
Storage.Flush();

// act
var serializedStream = JsonConvert.SerializeObject(Storage);
var mutableStream = JsonConvert.DeserializeObject<ConcurrentStream>(serializedStream);
Storage.Dispose();
using var mutableStream = JsonConvert.DeserializeObject<ConcurrentStream>(serializedStream);
await mutableStream.WriteAsync(mutableStream.Position, data, data.Length);
mutableStream.Flush();

Expand All @@ -214,8 +215,8 @@ public async Task TestStreamExpandability()
public async Task TestDynamicBufferData()
{
// arrange
CreateStorage(DataLength);
var size = 1024; // 1KB
CreateStorage(size);

// act
for (int i = 0; i < size / 8; i++)
Expand Down Expand Up @@ -245,15 +246,16 @@ public async Task TestDynamicBufferData()
public async Task TestSerialization()
{
// arrange
CreateStorage(DataLength);
var size = 256;
var data = DummyData.GenerateOrderedBytes(size);
CreateStorage(size);
await Storage.WriteAsync(0, data, size);
Storage.Flush();

// act
await Storage.WriteAsync(0, data, size);
var serializedStream = JsonConvert.SerializeObject(Storage);
Storage.Dispose();
var newStream = JsonConvert.DeserializeObject<ConcurrentStream>(serializedStream);
using var newStream = JsonConvert.DeserializeObject<ConcurrentStream>(serializedStream);
var readerStream = newStream.OpenRead();

// assert
Expand Down
5 changes: 4 additions & 1 deletion src/Downloader/ConcurrentStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public byte[] Data
{
// Don't pass straight value to MemoryStream,
// because causes stream to be an immutable array
_stream = new MemoryStream();
_stream = new MemoryStream();
_stream.Write(value, 0, value.Length);
}
}
Expand Down Expand Up @@ -115,6 +115,9 @@ public int Read(byte[] buffer, int offset, int count)

public async Task WriteAsync(long position, byte[] bytes, int length)
{
if (bytes.Length < length)
throw new ArgumentOutOfRangeException(nameof(length));

await _inputBuffer.TryAdd(new Packet(position, bytes, length));
}

Expand Down

0 comments on commit c398569

Please sign in to comment.