Skip to content

Commit

Permalink
Fix code analysis nits (#553)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpassing authored Sep 1, 2021
1 parent 0bbca5d commit 98b5713
Show file tree
Hide file tree
Showing 16 changed files with 233 additions and 170 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,19 @@ public async Task WhenUsingExistingKey_ThenAddInstanceMetadataSucceeds(
var key = Guid.NewGuid().ToString();

await this.instancesResource.AddMetadataAsync(
locator,
key,
"value to be overridden",
CancellationToken.None);
locator,
key,
"value to be overridden",
CancellationToken.None)
.ConfigureAwait(false);

var value = "metadata value";
await this.instancesResource.AddMetadataAsync(
locator,
key,
value,
CancellationToken.None);
locator,
key,
value,
CancellationToken.None)
.ConfigureAwait(false);

var instance = await this.instancesResource.Get(
locator.ProjectId,
Expand All @@ -122,23 +124,24 @@ public async Task WhenUpdateConflictingOnFirstAttempt_ThenUpdateInstanceMetadata

int callbacks = 0;
await this.instancesResource.UpdateMetadataAsync(
locator,
metadata =>
{
if (callbacks++ == 0)
locator,
metadata =>
{
// Provoke a conflict on the first attempt.
this.instancesResource.AddMetadataAsync(
locator,
key,
"conflict #" + callbacks,
CancellationToken.None).Wait();
}
if (callbacks++ == 0)
{
// Provoke a conflict on the first attempt.
this.instancesResource.AddMetadataAsync(
locator,
key,
"conflict #" + callbacks,
CancellationToken.None).Wait();
}

metadata.Add(key, "value");
},
CancellationToken.None,
2);
metadata.Add(key, "value");
},
CancellationToken.None,
2)
.ConfigureAwait(false);

var instance = await this.instancesResource.Get(
locator.ProjectId,
Expand Down Expand Up @@ -190,10 +193,11 @@ public async Task WhenUsingNewKey_ThenAddProjectMetadataSucceeds()
var value = "metadata value";

await this.projectsResource.AddMetadataAsync(
TestProject.ProjectId,
key,
value,
CancellationToken.None);
TestProject.ProjectId,
key,
value,
CancellationToken.None)
.ConfigureAwait(false);

var project = await this.projectsResource.Get(TestProject.ProjectId)
.ExecuteAsync(CancellationToken.None)
Expand All @@ -210,17 +214,19 @@ public async Task WhenUsingExistingKey_ThenAddProjectMetadataSucceeds()
var key = Guid.NewGuid().ToString();

await this.projectsResource.AddMetadataAsync(
TestProject.ProjectId,
key,
"value to be overridden",
CancellationToken.None);
TestProject.ProjectId,
key,
"value to be overridden",
CancellationToken.None)
.ConfigureAwait(false);

var value = "metadata value";
await this.projectsResource.AddMetadataAsync(
TestProject.ProjectId,
key,
value,
CancellationToken.None);
TestProject.ProjectId,
key,
value,
CancellationToken.None)
.ConfigureAwait(false);

var project = await this.projectsResource.Get(TestProject.ProjectId)
.ExecuteAsync(CancellationToken.None)
Expand All @@ -238,23 +244,24 @@ public async Task WhenUpdateConflictingOnFirstAttempt_ThenUpdateProjectMetadataR

int callbacks = 0;
await this.projectsResource.UpdateMetadataAsync(
TestProject.ProjectId,
metadata =>
{
if (callbacks++ == 0)
TestProject.ProjectId,
metadata =>
{
// Provoke a conflict on the first attempt.
this.projectsResource.AddMetadataAsync(
TestProject.ProjectId,
key,
"conflict #" + callbacks,
CancellationToken.None).Wait();
}
if (callbacks++ == 0)
{
// Provoke a conflict on the first attempt.
this.projectsResource.AddMetadataAsync(
TestProject.ProjectId,
key,
"conflict #" + callbacks,
CancellationToken.None).Wait();
}

metadata.Add(key, "value");
},
CancellationToken.None,
2);
metadata.Add(key, "value");
},
CancellationToken.None,
2)
.ConfigureAwait(false);

var project = await this.projectsResource.Get(TestProject.ProjectId)
.ExecuteAsync(CancellationToken.None)
Expand Down
15 changes: 9 additions & 6 deletions sources/Google.Solutions.Common.Test/Auth/TestAuthorization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ public async Task WhenNoExistingAuthPresent_TryLoadExistingAuthorizationAsyncRet
.Returns(Task.FromResult<TokenResponse>(null));

var authz = await OAuthAuthorization.TryLoadExistingAuthorizationAsync(
adapter.Object,
CancellationToken.None);
adapter.Object,
CancellationToken.None)
.ConfigureAwait(false);

Assert.IsNull(authz);
}
Expand All @@ -64,8 +65,9 @@ public async Task WhenExistingAuthLacksScopes_TryLoadExistingAuthorizationAsyncR
.Returns(new[] { "one", "two", "email" });

var authz = await OAuthAuthorization.TryLoadExistingAuthorizationAsync(
adapter.Object,
CancellationToken.None);
adapter.Object,
CancellationToken.None)
.ConfigureAwait(false);

Assert.IsNull(authz);

Expand All @@ -90,8 +92,9 @@ public async Task WhenExistingAuthIsOk_TryLoadExistingAuthorizationAsyncReturnsA
.Returns(new[] { "one", "two", "email" });

var authz = await OAuthAuthorization.TryLoadExistingAuthorizationAsync(
adapter.Object,
CancellationToken.None);
adapter.Object,
CancellationToken.None)
.ConfigureAwait(false);

Assert.IsNotNull(authz);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,16 @@ public static async Task<ICredential> CreateServiceAccountCredentialAsync(
try
{
// Create a service account.
var serviceAccount = await CreateOrGetServiceAccountAsync(name);
var serviceAccount = await CreateOrGetServiceAccountAsync(name)
.ConfigureAwait(true);

// Assign roles.
await GrantRolesToServiceAccountAsync(serviceAccount, roles);
await GrantRolesToServiceAccountAsync(serviceAccount, roles)
.ConfigureAwait(true);

// Create a token.
return await CreateTemporaryCredentialsAsync(serviceAccount.Email);
return await CreateTemporaryCredentialsAsync(serviceAccount.Email)
.ConfigureAwait(true);
}
catch (Exception e)
{
Expand Down
88 changes: 49 additions & 39 deletions sources/Google.Solutions.Common.Test/Integration/InstanceFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ private static async Task AwaitInstanceCreatedAndReady(
locator.ProjectId,
locator.Zone,
locator.Name)
.ExecuteAsync();
.ExecuteAsync()
.ConfigureAwait(true);

// Determine the name of the guest attribute we need to await.
var guestAttributeToAwait = instance.Metadata.Items
Expand Down Expand Up @@ -138,51 +139,55 @@ public static async Task<InstanceLocator> CreateOrStartInstanceAsync(
{
new ServiceAccount()
{
Email = await GetComputeEngineDefaultServiceAccount(),
Email = await GetComputeEngineDefaultServiceAccount()
.ConfigureAwait(true),
Scopes = new [] { "https://www.googleapis.com/auth/cloud-platform" }
}
};
}

await computeEngine.Instances.Insert(
new Apis.Compute.v1.Data.Instance()
{
Name = name,
MachineType = $"zones/{locator.Zone}/machineTypes/{machineType}",
Disks = new[]
new Apis.Compute.v1.Data.Instance()
{
new AttachedDisk()
Name = name,
MachineType = $"zones/{locator.Zone}/machineTypes/{machineType}",
Disks = new[]
{
AutoDelete = true,
Boot = true,
InitializeParams = new AttachedDiskInitializeParams()
new AttachedDisk()
{
SourceImage = imageFamily
AutoDelete = true,
Boot = true,
InitializeParams = new AttachedDiskInitializeParams()
{
SourceImage = imageFamily
}
}
}
},
Metadata = metadata,
NetworkInterfaces = new[]
{
new NetworkInterface()
},
Metadata = metadata,
NetworkInterfaces = new[]
{
AccessConfigs = publicIp
? new [] { new AccessConfig() }
: null
}
},
Scheduling = new Scheduling()
{
Preemptible = true
new NetworkInterface()
{
AccessConfigs = publicIp
? new [] { new AccessConfig() }
: null
}
},
Scheduling = new Scheduling()
{
Preemptible = true
},
ServiceAccounts = serviceAccounts
},
ServiceAccounts = serviceAccounts
},
locator.ProjectId,
locator.Zone).ExecuteAsync();
locator.ProjectId,
locator.Zone)
.ExecuteAsync()
.ConfigureAwait(true);

await AwaitInstanceCreatedAndReady(
computeEngine.Instances,
locator);
computeEngine.Instances,
locator)
.ConfigureAwait(true);

return locator;
}
Expand All @@ -194,7 +199,8 @@ await AwaitInstanceCreatedAndReady(
locator.ProjectId,
locator.Zone,
locator.Name)
.ExecuteAsync();
.ExecuteAsync()
.ConfigureAwait(true);

if (instance.Status == "RUNNING" ||
instance.Status == "PROVISIONING" ||
Expand All @@ -204,8 +210,9 @@ await AwaitInstanceCreatedAndReady(
"Instance {0} exists and is running...", locator.Name);

await AwaitInstanceCreatedAndReady(
computeEngine.Instances,
locator);
computeEngine.Instances,
locator)
.ConfigureAwait(true);
return locator;
}
else if (instance.Status == "TERMINATED")
Expand All @@ -217,17 +224,20 @@ await AwaitInstanceCreatedAndReady(
await computeEngine.Instances.AddMetadataAsync(
locator,
metadata,
CancellationToken.None);
CancellationToken.None)
.ConfigureAwait(true);

await computeEngine.Instances.Start(
locator.ProjectId,
locator.Zone,
locator.Name)
.ExecuteAsync();
.ExecuteAsync()
.ConfigureAwait(true);

await AwaitInstanceCreatedAndReady(
computeEngine.Instances,
locator);
computeEngine.Instances,
locator)
.ConfigureAwait(true);
return locator;
}
else
Expand Down
12 changes: 7 additions & 5 deletions sources/Google.Solutions.Common.Test/Net/InprocHttpProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,11 @@ private async Task DispatchRequestAsync(NetworkStream clientStream)
this.connectionTargets.AddLast(matchConnect.Groups[1].Value);

await DispatchRequestAsync(
matchConnect.Groups[1].Value,
ushort.Parse(matchConnect.Groups[2].Value),
headers,
clientStream);
matchConnect.Groups[1].Value,
ushort.Parse(matchConnect.Groups[2].Value),
headers,
clientStream)
.ConfigureAwait(true);
}
else if (GetRequestPattern.Match(firstLine) is Match getMatch &&
getMatch.Success &&
Expand Down Expand Up @@ -236,7 +237,8 @@ protected override async Task DispatchRequestAsync(
}
else
{
await base.DispatchRequestAsync(server, serverPort, headers, clientStream);
await base.DispatchRequestAsync(server, serverPort, headers, clientStream)
.ConfigureAwait(true);
}
}
else
Expand Down
5 changes: 3 additions & 2 deletions sources/Google.Solutions.Common.Test/Net/TestRestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ public async Task WhenUrlPointsToJson_ThenGetAsyncReturnsObject()
{
var client = new RestClient();
var result = await client.GetAsync<SampleResource>(
SampleRestUrl,
CancellationToken.None);
SampleRestUrl,
CancellationToken.None)
.ConfigureAwait(false);

Assert.IsNotNull(result.Issuer);
}
Expand Down
Loading

0 comments on commit 98b5713

Please sign in to comment.