Skip to content
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

Added cancellation token to async methods #406

Merged
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using ArangoDBNetStandard;
using ArangoDBNetStandard.CollectionApi;
Expand Down Expand Up @@ -159,8 +160,9 @@ public async Task PostCollectionAsync_ShouldUseQueryParameter()
mockTransport.Setup(x => x.PostAsync(
It.IsAny<string>(),
It.IsAny<byte[]>(),
It.IsAny<WebHeaderCollection>()))
.Returns((string uri, byte[] content, WebHeaderCollection webHeaderCollection) =>
It.IsAny<WebHeaderCollection>(),
It.IsAny<CancellationToken>()))
.Returns((string uri, byte[] content, WebHeaderCollection webHeaderCollection, CancellationToken token) =>
{
requestUri = uri;
return Task.FromResult(mockResponse.Object);
Expand Down
9 changes: 6 additions & 3 deletions arangodb-net-standard.Test/CursorApi/CursorApiClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using ArangoDBNetStandard;
using ArangoDBNetStandard.CursorApi;
Expand Down Expand Up @@ -188,7 +189,8 @@ public async Task PostCursorAsync_ShouldThrow_WhenErrorDeserializationFailed()
mockTransport.Setup(x => x.PostAsync(
It.IsAny<string>(),
It.IsAny<byte[]>(),
It.IsAny<WebHeaderCollection>()))
It.IsAny<WebHeaderCollection>(),
It.IsAny<CancellationToken>()))
.Returns(Task.FromResult(mockResponse.Object));

var cursorApi = new CursorApiClient(mockTransport.Object);
Expand Down Expand Up @@ -239,8 +241,9 @@ public async Task PostCursorAsync_ShouldUseHeaderProperties()
mockTransport.Setup(x => x.PostAsync(
It.IsAny<string>(),
It.IsAny<byte[]>(),
It.IsAny<WebHeaderCollection>()))
.Returns((string uri, byte[] content, WebHeaderCollection webHeaderCollection) =>
It.IsAny<WebHeaderCollection>(),
It.IsAny<CancellationToken>()))
.Returns((string uri, byte[] content, WebHeaderCollection webHeaderCollection, CancellationToken token) =>
{
requestHeader = webHeaderCollection;
return Task.FromResult(mockResponse.Object);
Expand Down
33 changes: 23 additions & 10 deletions arangodb-net-standard.Test/DocumentApi/DocumentApiClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using ArangoDBNetStandard;
using ArangoDBNetStandard.DocumentApi;
Expand Down Expand Up @@ -109,8 +110,11 @@ public async Task DeleteDocument_ShouldUseQueryParameters_WhenProvided()

string requestUri = null;

mockTransport.Setup(x => x.DeleteAsync(It.IsAny<string>(), It.IsAny<WebHeaderCollection>()))
.Returns((string uri, WebHeaderCollection webHeaderCollection) =>
mockTransport.Setup(x => x.DeleteAsync(
It.IsAny<string>(),
It.IsAny<WebHeaderCollection>(),
It.IsAny<CancellationToken>()))
.Returns((string uri, WebHeaderCollection webHeaderCollection, CancellationToken token) =>
{
requestUri = uri;
return Task.FromResult(mockResponse.Object);
Expand Down Expand Up @@ -290,8 +294,10 @@ public async Task DeleteDocuments_ShouldUseQueryParameters_WhenProvided()

string requestUri = null;

mockTransport.Setup(x => x.DeleteAsync(It.IsAny<string>(), It.IsAny<byte[]>(), It.IsAny<WebHeaderCollection>()))
.Returns((string uri, byte[] content, WebHeaderCollection webHeaderCollection) =>
mockTransport.Setup(x => x.DeleteAsync(It.IsAny<string>(), It.IsAny<byte[]>(),
It.IsAny<WebHeaderCollection>(),
It.IsAny<CancellationToken>()))
.Returns((string uri, byte[] content, WebHeaderCollection webHeaderCollection, CancellationToken token) =>
{
requestUri = uri;
return Task.FromResult(mockResponse.Object);
Expand Down Expand Up @@ -620,8 +626,10 @@ public async Task PutDocument_ShouldUseQueryParameters_WhenProvided()

string requestUri = null;

mockTransport.Setup(x => x.PutAsync(It.IsAny<string>(), It.IsAny<byte[]>(), It.IsAny<WebHeaderCollection>()))
.Returns((string uri, byte[] content, WebHeaderCollection webHeaderCollection) =>
mockTransport.Setup(x => x.PutAsync(It.IsAny<string>(), It.IsAny<byte[]>(),
It.IsAny<WebHeaderCollection>(),
It.IsAny<CancellationToken>()))
.Returns((string uri, byte[] content, WebHeaderCollection webHeaderCollection, CancellationToken token) =>
{
requestUri = uri;
return Task.FromResult(mockResponse.Object);
Expand Down Expand Up @@ -726,8 +734,10 @@ public async Task PutDocumentsAsync_ShouldUseQueryParameters_WhenProvided()

string requestUri = null;

mockTransport.Setup(x => x.PutAsync(It.IsAny<string>(), It.IsAny<byte[]>(), It.IsAny<WebHeaderCollection>()))
.Returns((string uri, byte[] content, WebHeaderCollection webHeaderCollection) =>
mockTransport.Setup(x => x.PutAsync(It.IsAny<string>(), It.IsAny<byte[]>(),
It.IsAny<WebHeaderCollection>(),
It.IsAny<CancellationToken>()))
.Returns((string uri, byte[] content, WebHeaderCollection webHeaderCollection, CancellationToken token) =>
{
requestUri = uri;
return Task.FromResult(mockResponse.Object);
Expand Down Expand Up @@ -853,8 +863,11 @@ public async Task PatchDocumentsAsync_ShouldUseQueryParameters_WhenProvided()

string requestUri = null;

mockTransport.Setup(x => x.PatchAsync(It.IsAny<string>(), It.IsAny<byte[]>(), It.IsAny<WebHeaderCollection>()))
.Returns((string uri, byte[] content, WebHeaderCollection webHeaderCollection) =>
mockTransport.Setup(x => x.PatchAsync(
It.IsAny<string>(), It.IsAny<byte[]>(),
It.IsAny<WebHeaderCollection>(),
It.IsAny<CancellationToken>()))
.Returns((string uri, byte[] content, WebHeaderCollection webHeaderCollection, CancellationToken token) =>
{
requestUri = uri;
return Task.FromResult(mockResponse.Object);
Expand Down
22 changes: 16 additions & 6 deletions arangodb-net-standard.Test/UserApi/UserApiClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Xunit;

Expand Down Expand Up @@ -291,8 +292,11 @@ public async Task GetDatabaseAccessLevelAsync_ShouldThrow_WhenErrorResponseRetur

string requestUri = null;

mockTransport.Setup(x => x.GetAsync(It.IsAny<string>(), It.IsAny<WebHeaderCollection>()))
.Returns((string uri, WebHeaderCollection webHeaderCollection) =>
mockTransport.Setup(x => x.GetAsync(
It.IsAny<string>(),
It.IsAny<WebHeaderCollection>(),
It.IsAny<CancellationToken>()))
.Returns((string uri, WebHeaderCollection webHeaderCollection, CancellationToken token) =>
{
requestUri = uri;
return Task.FromResult(mockResponse.Object);
Expand Down Expand Up @@ -375,8 +379,11 @@ public async Task GetAccessibleDatabasesAsync_ShouldUseQueryParameters_WhenProvi

string requestUri = null;

mockTransport.Setup(x => x.GetAsync(It.IsAny<string>(), It.IsAny<WebHeaderCollection>()))
.Returns((string uri, WebHeaderCollection webHeaderCollection) =>
mockTransport.Setup(x => x.GetAsync(
It.IsAny<string>(),
It.IsAny<WebHeaderCollection>(),
It.IsAny<CancellationToken>()))
.Returns((string uri, WebHeaderCollection webHeaderCollection, CancellationToken token) =>
{
requestUri = uri;
return Task.FromResult(mockResponse.Object);
Expand Down Expand Up @@ -512,8 +519,11 @@ public async Task GetCollectionAccessLevelAsync_ShouldThrow_WhenErrorResponseRet

string requestUri = null;

mockTransport.Setup(x => x.GetAsync(It.IsAny<string>(), It.IsAny<WebHeaderCollection>()))
.Returns((string uri, WebHeaderCollection webHeaderCollection) =>
mockTransport.Setup(x => x.GetAsync(
It.IsAny<string>(),
It.IsAny<WebHeaderCollection>(),
It.IsAny<CancellationToken>()))
.Returns((string uri, WebHeaderCollection webHeaderCollection, CancellationToken token) =>
{
requestUri = uri;
return Task.FromResult(mockResponse.Object);
Expand Down
31 changes: 19 additions & 12 deletions arangodb-net-standard/AdminApi/AdminApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using ArangoDBNetStandard.Serialization;
using ArangoDBNetStandard.Transport;
using ArangoDBNetStandard.AdminApi.Models;
using System.Threading;

namespace ArangoDBNetStandard.AdminApi
{
Expand Down Expand Up @@ -50,20 +51,21 @@ public AdminApiClient(IApiClientTransport client, IApiClientSerialization serial
/// GET /_admin/log/entries
/// Works on ArangoDB 3.8 or later.
/// </summary>
/// <param name="token">A CancellationToken to observe while waiting for the task to complete or to cancel the task.</param>
/// <param name="query">Query string parameters</param>
/// <returns></returns>
/// <remarks>
/// For further information see
/// https://www.arangodb.com/docs/stable/http/administration-and-monitoring.html#read-global-logs-from-the-server
/// </remarks>
public virtual async Task<GetLogsResponse> GetLogsAsync(GetLogsQuery query = null)
public virtual async Task<GetLogsResponse> GetLogsAsync(GetLogsQuery query = null, CancellationToken token = default)
{
string uri = $"{_adminApiPath}/log/entries";
if (query != null)
{
uri += '?' + query.ToQueryString();
}
using (var response = await _client.GetAsync(uri).ConfigureAwait(false))
using (var response = await _client.GetAsync(uri, null, token).ConfigureAwait(false))
{
if (response.IsSuccessStatusCode)
{
Expand All @@ -78,16 +80,17 @@ public virtual async Task<GetLogsResponse> GetLogsAsync(GetLogsQuery query = nul
/// Reloads the routing table.
/// POST /_admin/routing/reload
/// </summary>
/// <param name="token">A CancellationToken to observe while waiting for the task to complete or to cancel the task.</param>
/// <returns></returns>
/// <remarks>
/// For further information see
/// https://www.arangodb.com/docs/stable/http/administration-and-monitoring.html#reloads-the-routing-information
/// </remarks>
public virtual async Task<bool> PostReloadRoutingInfoAsync()
public virtual async Task<bool> PostReloadRoutingInfoAsync(CancellationToken token = default)
{
string uri = $"{_adminApiPath}/routing/reload";
var body = new byte[] { };
using (var response = await _client.PostAsync(uri, body).ConfigureAwait(false))
using (var response = await _client.PostAsync(uri, body, null, token).ConfigureAwait(false))
{
if (response.IsSuccessStatusCode)
{
Expand All @@ -102,15 +105,16 @@ public virtual async Task<bool> PostReloadRoutingInfoAsync()
/// The method will fail if the server is not running in cluster mode.
/// GET /_admin/server/id
/// </summary>
/// <param name="token">A CancellationToken to observe while waiting for the task to complete or to cancel the task.</param>
/// <returns></returns>
/// <remarks>
/// For further information see
/// https://www.arangodb.com/docs/stable/http/administration-and-monitoring.html#return-id-of-a-server-in-a-cluster
/// </remarks>
public virtual async Task<GetServerIdResponse> GetServerIdAsync()
public virtual async Task<GetServerIdResponse> GetServerIdAsync(CancellationToken token = default)
{
string uri = $"{_adminApiPath}/server/id";
using (var response = await _client.GetAsync(uri).ConfigureAwait(false))
using (var response = await _client.GetAsync(uri, null, token).ConfigureAwait(false))
{
if (response.IsSuccessStatusCode)
{
Expand All @@ -125,15 +129,16 @@ public virtual async Task<GetServerIdResponse> GetServerIdAsync()
/// Retrieves the role of the server in a cluster.
/// GET /_admin/server/role
/// </summary>
/// <param name="token">A CancellationToken to observe while waiting for the task to complete or to cancel the task.</param>
/// <returns></returns>
/// <remarks>
/// For further information see
/// https://www.arangodb.com/docs/stable/http/administration-and-monitoring.html#return-the-role-of-a-server-in-a-cluster
/// </remarks>
public virtual async Task<GetServerRoleResponse> GetServerRoleAsync()
public virtual async Task<GetServerRoleResponse> GetServerRoleAsync(CancellationToken token = default)
{
string uri = $"{_adminApiPath}/server/role";
using (var response = await _client.GetAsync(uri).ConfigureAwait(false))
using (var response = await _client.GetAsync(uri, null, token).ConfigureAwait(false))
{
if (response.IsSuccessStatusCode)
{
Expand All @@ -148,15 +153,16 @@ public virtual async Task<GetServerRoleResponse> GetServerRoleAsync()
/// Retrieves the server database engine type.
/// GET /_api/engine
/// </summary>
/// <param name="token">A CancellationToken to observe while waiting for the task to complete or to cancel the task.</param>
/// <returns></returns>
/// <remarks>
/// For further information see
/// https://www.arangodb.com/docs/stable/http/miscellaneous-functions.html#return-server-database-engine-type
/// </remarks>
public virtual async Task<GetServerEngineTypeResponse> GetServerEngineTypeAsync()
public virtual async Task<GetServerEngineTypeResponse> GetServerEngineTypeAsync(CancellationToken token = default)
{
string uri = "_api/engine";
using (var response = await _client.GetAsync(uri).ConfigureAwait(false))
using (var response = await _client.GetAsync(uri, null, token).ConfigureAwait(false))
{
if (response.IsSuccessStatusCode)
{
Expand All @@ -172,19 +178,20 @@ public virtual async Task<GetServerEngineTypeResponse> GetServerEngineTypeAsync(
/// GET /_api/version
/// </summary>
/// <param name="query">Query string parameters</param>
/// <param name="token">A CancellationToken to observe while waiting for the task to complete or to cancel the task.</param>
/// <returns></returns>
/// <remarks>
/// For further information see
/// https://www.arangodb.com/docs/stable/http/miscellaneous-functions.html#return-server-version
/// </remarks>
public virtual async Task<GetServerVersionResponse> GetServerVersionAsync(GetServerVersionQuery query = null)
public virtual async Task<GetServerVersionResponse> GetServerVersionAsync(GetServerVersionQuery query = null, CancellationToken token = default)
{
string uri = "_api/version";
if (query != null)
{
uri += '?' + query.ToQueryString();
}
using (var response = await _client.GetAsync(uri).ConfigureAwait(false))
using (var response = await _client.GetAsync(uri,null,token).ConfigureAwait(false))
{
if (response.IsSuccessStatusCode)
{
Expand Down
Loading