Skip to content

Commit acddd18

Browse files
committed
Make use of new ipfscore package. Various analyzer updates
1 parent c54af88 commit acddd18

10 files changed

+118
-94
lines changed

src/CoreApi/PubSubApi.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,5 @@ void ProcessMessages(Action<PublishedMessage> handler, StreamReader sr, Cancella
114114
sr.Dispose();
115115
}
116116
}
117-
118117
}
119-
120118
}

src/IpfsClient.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ namespace Ipfs.Http
2626
/// </remarks>
2727
public partial class IpfsClient : ICoreApi
2828
{
29-
const string unknownFilename = "unknown";
29+
private const string unknownFilename = "unknown";
3030

31-
static readonly object safe = new object();
32-
static HttpClient? api = null;
31+
private static readonly object safe = new object();
32+
private static HttpClient? api;
3333

3434
/// <summary>
3535
/// The default URL to the IPFS HTTP API server.
@@ -42,7 +42,7 @@ public partial class IpfsClient : ICoreApi
4242
/// </remarks>
4343
public static Uri DefaultApiUri = new Uri(
4444
Environment.GetEnvironmentVariable("IpfsHttpApi")
45-
?? "http://localhost:5001");
45+
?? "http://localhost:1206");
4646

4747
/// <summary>
4848
/// Creates a new instance of the <see cref="IpfsClient"/> class and sets the
@@ -493,6 +493,7 @@ public async Task<String> UploadAsync(string command, CancellationToken cancel,
493493
return json;
494494
}
495495
}
496+
496497
/// <summary>
497498
/// Perform an <see href="https://ipfs.io/docs/api/">IPFS API command</see> that
498499
/// requires uploading of a "file".

src/IpfsHttpClient.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<DebugType>full</DebugType>
99

1010
<!-- https://semver.org/spec/v2.0.0.html -->
11-
<Version>0.0.6</Version>
11+
<Version>0.0.7</Version>
1212
<AssemblyVersion>$(Version)</AssemblyVersion>
1313

1414
<!-- Nuget specs -->
@@ -45,7 +45,7 @@
4545
</ItemGroup>
4646

4747
<ItemGroup>
48-
<PackageReference Include="IpfsShipyard.Ipfs.Core" Version="0.0.4" />
48+
<PackageReference Include="IpfsShipyard.Ipfs.Core" Version="0.0.5" />
4949
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
5050
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
5151
<PackageReference Include="Multiformats.Base" Version="2.0.2" />

test/BlockTest.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,5 @@ public void DataStream()
3030
Assert.AreEqual(3, stream.ReadByte());
3131
Assert.AreEqual(-1, stream.ReadByte(), "at eof");
3232
}
33-
3433
}
3534
}

test/CoreApi/BitswapApiTest.cs

Lines changed: 67 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,71 @@
1-
using Microsoft.VisualStudio.TestTools.UnitTesting;
2-
using System;
1+
using System;
32
using System.Linq;
43
using System.Text;
4+
using System.Threading;
55
using System.Threading.Tasks;
6+
using Microsoft.VisualStudio.TestTools.UnitTesting;
67

78
namespace Ipfs.Http
89
{
910
[TestClass]
10-
public class BitswapApiTest
11+
public sealed class BitswapApiTest
1112
{
1213
private readonly IpfsClient ipfs = TestFixture.Ipfs;
1314

1415
[TestMethod]
1516
public async Task Wants()
1617
{
1718
var block = new DagNode(Encoding.UTF8.GetBytes("BitswapApiTest unknown block"));
18-
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
19-
Task.Run(() => ipfs.Bitswap.GetAsync(block.Id).Wait());
20-
21-
var endTime = DateTime.Now.AddSeconds(10);
22-
while (DateTime.Now < endTime)
23-
{
24-
await Task.Delay(100);
25-
var wants = await ipfs.Bitswap.WantsAsync();
26-
if (wants.Contains(block.Id))
27-
return;
28-
}
29-
Assert.Fail("wanted block is missing");
19+
await RunAsyncTaskAndTestAsync(
20+
ct => ipfs.Bitswap.GetAsync(block.Id, ct),
21+
async () =>
22+
{
23+
var endTime = DateTime.Now.AddSeconds(10);
24+
while (DateTime.Now < endTime)
25+
{
26+
await Task.Delay(100);
27+
var wants = await ipfs.Bitswap.WantsAsync();
28+
if (wants.Contains(block.Id))
29+
{
30+
return;
31+
}
32+
}
33+
Assert.Fail("wanted block is missing");
34+
});
3035
}
3136

3237
[TestMethod]
3338
[Ignore("https://github.com/ipfs/go-ipfs/issues/5295")]
3439
public async Task Unwant()
3540
{
3641
var block = new DagNode(Encoding.UTF8.GetBytes("BitswapApiTest unknown block 2"));
37-
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
38-
Task.Run(() => ipfs.Bitswap.GetAsync(block.Id).Wait());
42+
await RunAsyncTaskAndTestAsync(
43+
ct => ipfs.Bitswap.GetAsync(block.Id, ct),
44+
async () =>
45+
{
46+
var endTime = DateTime.Now.AddSeconds(10);
47+
while (true)
48+
{
49+
if (DateTime.Now > endTime)
50+
Assert.Fail("wanted block is missing");
51+
await Task.Delay(100);
52+
var wants = await ipfs.Bitswap.WantsAsync();
53+
if (wants.Contains(block.Id))
54+
break;
55+
}
3956

40-
var endTime = DateTime.Now.AddSeconds(10);
41-
while (true)
42-
{
43-
if (DateTime.Now > endTime)
44-
Assert.Fail("wanted block is missing");
45-
await Task.Delay(100);
46-
var wants = await ipfs.Bitswap.WantsAsync();
47-
if (wants.Contains(block.Id))
48-
break;
49-
}
50-
51-
await ipfs.Bitswap.UnwantAsync(block.Id);
52-
endTime = DateTime.Now.AddSeconds(10);
53-
while (true)
54-
{
55-
if (DateTime.Now > endTime)
56-
Assert.Fail("unwanted block is present");
57-
await Task.Delay(100);
58-
var wants = await ipfs.Bitswap.WantsAsync();
59-
if (!wants.Contains(block.Id))
60-
break;
61-
}
57+
await ipfs.Bitswap.UnwantAsync(block.Id);
58+
endTime = DateTime.Now.AddSeconds(10);
59+
while (true)
60+
{
61+
if (DateTime.Now > endTime)
62+
Assert.Fail("unwanted block is present");
63+
await Task.Delay(100);
64+
var wants = await ipfs.Bitswap.WantsAsync();
65+
if (!wants.Contains(block.Id))
66+
break;
67+
}
68+
});
6269
}
6370

6471
[TestMethod]
@@ -70,5 +77,26 @@ public async Task Ledger()
7077
Assert.IsNotNull(ledger.Peer);
7178
Assert.AreEqual(peer.Id, ledger.Peer!.Id);
7279
}
80+
81+
private static async Task RunAsyncTaskAndTestAsync(Func<CancellationToken, Task> asyncTaskWork, Func<Task> testWork)
82+
{
83+
var cts = new CancellationTokenSource();
84+
var asyncTask = Task.Run(async () => await asyncTaskWork(cts.Token));
85+
try
86+
{
87+
await testWork();
88+
}
89+
finally
90+
{
91+
cts.Cancel();
92+
try
93+
{
94+
await asyncTask;
95+
}
96+
catch
97+
{
98+
}
99+
}
100+
}
73101
}
74102
}

test/CoreApi/BlockApiTest.cs

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,108 +11,108 @@ namespace Ipfs.Http
1111
public class BlockApiTest
1212
{
1313
private readonly IpfsClient ipfs = TestFixture.Ipfs;
14-
private const string id = "QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ";
14+
private const string id = "bafkreiaxnnnb7qz2focittuqq3ya25q7rcv3bqynnczfzako47346wosmu";
1515
private readonly byte[] blob = Encoding.UTF8.GetBytes("blorb");
1616

1717
[TestMethod]
18-
public void Put_Bytes()
18+
public async Task Put_Bytes()
1919
{
20-
var cid = ipfs.Block.PutAsync(blob).Result;
20+
var cid = await ipfs.Block.PutAsync(blob);
2121
Assert.AreEqual(id, (string)cid);
2222

23-
var data = ipfs.Block.GetAsync(cid).Result;
23+
var data = await ipfs.Block.GetAsync(cid);
2424
Assert.AreEqual(blob.Length, data.Size);
2525
CollectionAssert.AreEqual(blob, data.DataBytes);
2626
}
2727

2828
[TestMethod]
29-
public void Put_Bytes_ContentType()
29+
public async Task Put_Bytes_ContentType()
3030
{
31-
var cid = ipfs.Block.PutAsync(blob, contentType: "raw").Result;
31+
var cid = await ipfs.Block.PutAsync(blob, contentType: "raw");
3232
Assert.AreEqual("bafkreiaxnnnb7qz2focittuqq3ya25q7rcv3bqynnczfzako47346wosmu", (string)cid);
3333

34-
var data = ipfs.Block.GetAsync(cid).Result;
34+
var data = await ipfs.Block.GetAsync(cid);
3535
Assert.AreEqual(blob.Length, data.Size);
3636
CollectionAssert.AreEqual(blob, data.DataBytes);
3737
}
3838

3939
[TestMethod]
40-
public void Put_Bytes_Hash()
40+
public async Task Put_Bytes_Hash()
4141
{
42-
var cid = ipfs.Block.PutAsync(blob, "raw", "sha2-512").Result;
42+
var cid = await ipfs.Block.PutAsync(blob, "raw", "sha2-512");
4343
Assert.AreEqual("bafkrgqelljziv4qfg5mefz36m2y3h6voaralnw6lwb4f53xcnrf4mlsykkn7vt6eno547tw5ygcz62kxrle45wnbmpbofo5tvu57jvuaf7k7e", (string)cid);
4444

45-
var data = ipfs.Block.GetAsync(cid).Result;
45+
var data = await ipfs.Block.GetAsync(cid);
4646
Assert.AreEqual(blob.Length, data.Size);
4747
CollectionAssert.AreEqual(blob, data.DataBytes);
4848
}
4949

5050
[TestMethod]
51-
public void Put_Bytes_Pinned()
51+
public async Task Put_Bytes_Pinned()
5252
{
5353
var data1 = new byte[] { 23, 24, 127 };
54-
var cid1 = ipfs.Block.PutAsync(data1, contentType: "raw", pin: true).Result;
55-
var pins = ipfs.Pin.ListAsync().Result;
54+
var cid1 = await ipfs.Block.PutAsync(data1, contentType: "raw", pin: true);
55+
var pins = await ipfs.Pin.ListAsync();
5656
Assert.IsTrue(pins.Any(pin => pin == cid1));
5757

5858
var data2 = new byte[] { 123, 124, 27 };
59-
var cid2 = ipfs.Block.PutAsync(data2, contentType: "raw", pin: false).Result;
60-
pins = ipfs.Pin.ListAsync().Result;
59+
var cid2 = await ipfs.Block.PutAsync(data2, contentType: "raw", pin: false);
60+
pins = await ipfs.Pin.ListAsync();
6161
Assert.IsFalse(pins.Any(pin => pin == cid2));
6262
}
6363

6464
[TestMethod]
65-
public void Put_Stream()
65+
public async Task Put_Stream()
6666
{
67-
var cid = ipfs.Block.PutAsync(new MemoryStream(blob)).Result;
67+
var cid = await ipfs.Block.PutAsync(new MemoryStream(blob));
6868
Assert.AreEqual(id, (string)cid);
6969

70-
var data = ipfs.Block.GetAsync(cid).Result;
70+
var data = await ipfs.Block.GetAsync(cid);
7171
Assert.AreEqual(blob.Length, data.Size);
7272
CollectionAssert.AreEqual(blob, data.DataBytes);
7373
}
7474

7575
[TestMethod]
76-
public void Put_Stream_ContentType()
76+
public async Task Put_Stream_ContentType()
7777
{
78-
var cid = ipfs.Block.PutAsync(new MemoryStream(blob), contentType: "raw").Result;
78+
var cid = await ipfs.Block.PutAsync(new MemoryStream(blob), contentType: "raw");
7979
Assert.AreEqual("bafkreiaxnnnb7qz2focittuqq3ya25q7rcv3bqynnczfzako47346wosmu", (string)cid);
8080

81-
var data = ipfs.Block.GetAsync(cid).Result;
81+
var data = await ipfs.Block.GetAsync(cid);
8282
Assert.AreEqual(blob.Length, data.Size);
8383
CollectionAssert.AreEqual(blob, data.DataBytes);
8484
}
8585

8686
[TestMethod]
87-
public void Put_Stream_Hash()
87+
public async Task Put_Stream_Hash()
8888
{
89-
var cid = ipfs.Block.PutAsync(new MemoryStream(blob), "raw", "sha2-512").Result;
89+
var cid = await ipfs.Block.PutAsync(new MemoryStream(blob), "raw", "sha2-512");
9090
Assert.AreEqual("bafkrgqelljziv4qfg5mefz36m2y3h6voaralnw6lwb4f53xcnrf4mlsykkn7vt6eno547tw5ygcz62kxrle45wnbmpbofo5tvu57jvuaf7k7e", (string)cid);
9191

92-
var data = ipfs.Block.GetAsync(cid).Result;
92+
var data = await ipfs.Block.GetAsync(cid);
9393
Assert.AreEqual(blob.Length, data.Size);
9494
CollectionAssert.AreEqual(blob, data.DataBytes);
9595
}
9696

9797
[TestMethod]
98-
public void Put_Stream_Pinned()
98+
public async Task Put_Stream_Pinned()
9999
{
100100
var data1 = new MemoryStream(new byte[] { 23, 24, 127 });
101-
var cid1 = ipfs.Block.PutAsync(data1, contentType: "raw", pin: true).Result;
102-
var pins = ipfs.Pin.ListAsync().Result;
101+
var cid1 = await ipfs.Block.PutAsync(data1, contentType: "raw", pin: true);
102+
var pins = await ipfs.Pin.ListAsync();
103103
Assert.IsTrue(pins.Any(pin => pin == cid1));
104104

105105
var data2 = new MemoryStream(new byte[] { 123, 124, 27 });
106-
var cid2 = ipfs.Block.PutAsync(data2, contentType: "raw", pin: false).Result;
107-
pins = ipfs.Pin.ListAsync().Result;
106+
var cid2 = await ipfs.Block.PutAsync(data2, contentType: "raw", pin: false);
107+
pins = await ipfs.Pin.ListAsync();
108108
Assert.IsFalse(pins.Any(pin => pin == cid2));
109109
}
110110

111111
[TestMethod]
112-
public void Get()
112+
public async Task Get()
113113
{
114-
var _ = ipfs.Block.PutAsync(blob).Result;
115-
var block = ipfs.Block.GetAsync(id).Result;
114+
var _ = await ipfs.Block.PutAsync(blob);
115+
var block = await ipfs.Block.GetAsync(id);
116116
Assert.AreEqual(id, (string)block.Id);
117117
CollectionAssert.AreEqual(blob, block.DataBytes);
118118
var blob1 = new byte[blob.Length];
@@ -121,18 +121,18 @@ public void Get()
121121
}
122122

123123
[TestMethod]
124-
public void Stat()
124+
public async Task Stat()
125125
{
126-
var _ = ipfs.Block.PutAsync(blob).Result;
127-
var info = ipfs.Block.StatAsync(id).Result;
126+
var _ = await ipfs.Block.PutAsync(blob);
127+
var info = await ipfs.Block.StatAsync(id);
128128
Assert.AreEqual(id, (string)info.Id);
129129
Assert.AreEqual(5, info.Size);
130130
}
131131

132132
[TestMethod]
133133
public async Task Remove()
134134
{
135-
var _ = ipfs.Block.PutAsync(blob).Result;
135+
var _ = await ipfs.Block.PutAsync(blob);
136136
var cid = await ipfs.Block.RemoveAsync(id);
137137
Assert.IsNotNull(cid);
138138
Assert.AreEqual(id, (string)cid!);
@@ -141,13 +141,13 @@ public async Task Remove()
141141
[TestMethod]
142142
public void Remove_Unknown()
143143
{
144-
ExceptionAssert.Throws<Exception>(() => { var _ = ipfs.Block.RemoveAsync("QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rFF").Result; });
144+
ExceptionAssert.Throws<Exception>(() => { var _ = ipfs.Block.RemoveAsync("QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rFF").GetAwaiter().GetResult(); });
145145
}
146146

147147
[TestMethod]
148148
public async Task Remove_Unknown_OK()
149149
{
150-
var cid = await ipfs.Block.RemoveAsync("QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rFF", true);
150+
var cid = await ipfs.Block.RemoveAsync("QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rFF", ignoreNonexistent: true);
151151
Assert.IsNull(cid);
152152
}
153153
}

test/CoreApi/BlockRepositoryTest.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,5 @@ public async Task Version()
2121
var version = await ipfs.BlockRepository.VersionAsync();
2222
Assert.IsFalse(string.IsNullOrWhiteSpace(version));
2323
}
24-
2524
}
2625
}

test/CoreApi/BootstrapTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ namespace Ipfs.Http
77
[TestClass]
88
public class BootstapApiTest
99
{
10-
IpfsClient ipfs = TestFixture.Ipfs;
11-
MultiAddress somewhere = "/ip4/127.0.0.1/tcp/4009/ipfs/QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ";
10+
private readonly IpfsClient ipfs = TestFixture.Ipfs;
11+
private readonly MultiAddress somewhere = "/ip4/127.0.0.1/tcp/4009/ipfs/QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ";
1212

1313
[TestMethod]
1414
public async Task Add_Remove()

0 commit comments

Comments
 (0)