Skip to content

Commit

Permalink
wip: testing for ldap exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
rvazarkar committed Aug 29, 2024
1 parent 1ebeebe commit cce2c5e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/CommonLib/LdapConnectionPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public async IAsyncEnumerable<LdapResult<IDirectoryObject>> Query(LdapQueryParam
}
try {
_log.LogTrace("Sending ldap request - {Info}", queryParameters.GetQueryInfo());
response = (SearchResponse)connectionWrapper.Connection.SendRequest(searchRequest);
response = (SearchResponse)connectionWrapper.SendRequest(searchRequest);

if (response != null) {
querySuccess = true;
Expand All @@ -114,6 +114,7 @@ public async IAsyncEnumerable<LdapResult<IDirectoryObject>> Query(LdapQueryParam
* since non-paged queries do not require same server connections
*/
queryRetryCount++;
_log.LogDebug("Query - Attempting to get new connection after ServerDown for query {Info}", queryParameters.GetQueryInfo());
ReleaseConnection(connectionWrapper, true);

for (var retryCount = 0; retryCount < MaxRetries; retryCount++) {
Expand Down Expand Up @@ -145,6 +146,7 @@ public async IAsyncEnumerable<LdapResult<IDirectoryObject>> Query(LdapQueryParam
*/
busyRetryCount++;
var backoffDelay = GetNextBackoff(busyRetryCount);
_log.LogDebug("Query - Executing {Time} second backoff for query {Info}", backoffDelay.TotalSeconds, queryParameters.GetQueryInfo());
await Task.Delay(backoffDelay, cancellationToken);
} catch (LdapException le) {
/*
Expand Down Expand Up @@ -228,7 +230,7 @@ public async IAsyncEnumerable<LdapResult<IDirectoryObject>> PagedQuery(LdapQuery
SearchResponse response = null;
try {
_log.LogTrace("Sending paged ldap request - {Info}", queryParameters.GetQueryInfo());
response = (SearchResponse)connectionWrapper.Connection.SendRequest(searchRequest);
response = (SearchResponse)connectionWrapper.SendRequest(searchRequest);
if (response != null) {
pageResponse = (PageResultResponseControl)response.Controls
.Where(x => x is PageResultResponseControl).DefaultIfEmpty(null).FirstOrDefault();
Expand Down Expand Up @@ -420,7 +422,7 @@ public async IAsyncEnumerable<Result<string>> RangedRetrieval(string distinguish
_log.LogTrace("RangedRetrieval entered semaphore with {Count} remaining for query {Info}", _semaphore.CurrentCount, queryParameters.GetQueryInfo());
}
try {
response = (SearchResponse)connectionWrapper.Connection.SendRequest(searchRequest);
response = (SearchResponse)connectionWrapper.SendRequest(searchRequest);
} catch (LdapException le) when (le.ErrorCode == (int)ResultCode.Busy && busyRetryCount < MaxRetries) {
busyRetryCount++;
var backoffDelay = GetNextBackoff(busyRetryCount);
Expand Down Expand Up @@ -587,7 +589,7 @@ private bool CallDsGetDcName(string domainName, out NetAPIStructs.DomainControll
return true;
}

public async Task<(bool Success, LdapConnectionWrapper ConnectionWrapper, string Message)> GetConnectionAsync() {
public virtual async Task<(bool Success, LdapConnectionWrapper ConnectionWrapper, string Message)> GetConnectionAsync() {
if (!_connections.TryTake(out var connectionWrapper)) {
var (success, connection, message) = await CreateNewConnection();
if (!success) {
Expand All @@ -605,11 +607,10 @@ private bool CallDsGetDcName(string domainName, out NetAPIStructs.DomainControll
return CreateNewConnectionForServer(server, globalCatalog);
}

public async Task<(bool Success, LdapConnectionWrapper ConnectionWrapper, string Message)> GetGlobalCatalogConnectionAsync() {
public virtual async Task<(bool Success, LdapConnectionWrapper ConnectionWrapper, string Message)> GetGlobalCatalogConnectionAsync() {
if (!_globalCatalogConnection.TryTake(out var connectionWrapper)) {
var (success, connection, message) = await CreateNewConnection(true);
if (!success) {
//If we didn't get a connection, immediately release the semaphore so we don't have hanging ones
return (false, null, message);
}

Expand Down
4 changes: 4 additions & 0 deletions src/CommonLib/LdapConnectionWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public string GetServer() {
return _server;
}

public virtual DirectoryResponse SendRequest(SearchRequest searchRequest) {
return Connection.SendRequest(searchRequest);
}

public bool GetSearchBase(NamingContext context, out string searchBase) {
searchBase = GetSavedContext(context);
if (searchBase != null) {
Expand Down
11 changes: 11 additions & 0 deletions test/unit/LdapConnectionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Threading.Tasks;
using Xunit;

namespace CommonLibTest;

public class LdapConnectionTests {
[Fact]
public async Task LdapConnection_TestExceptionHandling_ServerDown() {

Check warning on line 8 in test/unit/LdapConnectionTests.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

}
}

0 comments on commit cce2c5e

Please sign in to comment.