Skip to content

Commit 0338c62

Browse files
authored
Update DynamoDB tests to use async operations (#4203)
1 parent 0e14a25 commit 0338c62

File tree

9 files changed

+1179
-1443
lines changed

9 files changed

+1179
-1443
lines changed

sdk/test/Services/DynamoDBv2/IntegrationTests/AWSSDK.IntegrationTests.DynamoDBv2.NetFramework.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<GenerateAssemblyFileVersionAttribute>false</GenerateAssemblyFileVersionAttribute>
1717
<GenerateAssemblyDescriptionAttribute>false</GenerateAssemblyDescriptionAttribute>
1818
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
19-
<NoWarn>CS1591</NoWarn>
19+
<NoWarn>CS1591;CS0618</NoWarn>
2020
</PropertyGroup>
2121

2222
<ItemGroup>

sdk/test/Services/DynamoDBv2/IntegrationTests/CSMTest.cs

Lines changed: 0 additions & 54 deletions
This file was deleted.

sdk/test/Services/DynamoDBv2/IntegrationTests/Cache.cs

Lines changed: 40 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,61 @@
1-
using System;
2-
using System.Linq;
3-
using System.Threading;
4-
using Microsoft.VisualStudio.TestTools.UnitTesting;
5-
6-
using Amazon;
7-
using Amazon.Runtime;
8-
using Amazon.Runtime.Internal.Util;
9-
using Amazon.DynamoDBv2.Model;
1+
using Amazon;
102
using Amazon.DynamoDBv2;
11-
using System.Collections.Generic;
123
using Amazon.DynamoDBv2.DocumentModel;
4+
using Amazon.DynamoDBv2.Model;
5+
using Amazon.Runtime.Internal.Util;
136
using AWSSDK_DotNet.IntegrationTests.Utils;
14-
using System.Text;
15-
using System.IO;
16-
using AWSSDK_DotNet.IntegrationTests.Tests.DynamoDB;
7+
using Microsoft.VisualStudio.TestTools.UnitTesting;
8+
using System;
9+
using System.Collections.Generic;
10+
using System.Threading;
11+
using System.Threading.Tasks;
1712

18-
namespace AWSSDK_DotNet.IntegrationTests.Tests
13+
namespace AWSSDK_DotNet.IntegrationTests.Tests.DynamoDB
1914
{
2015
[TestClass]
2116
public class CacheTests
2217
{
2318
private const string TABLENAME = "cache-test-table";
2419
private string testTableName = null;
25-
private static AmazonDynamoDBClient client = AWSSDK_DotNet.IntegrationTests.Tests.DynamoDB.DynamoDBTests.Client;
20+
private static AmazonDynamoDBClient client = DynamoDBTests.Client;
2621
private static bool lastUseSdkCacheValue;
2722

2823
[TestInitialize]
29-
public void Init()
24+
public async Task Init()
3025
{
3126
lastUseSdkCacheValue = AWSConfigs.UseSdkCache;
3227
AWSConfigs.UseSdkCache = true;
3328
SdkCache.Clear();
3429

3530
testTableName = UtilityMethods.GenerateName("CacheTest");
36-
CreateTable(testTableName, true);
31+
await CreateTable(testTableName, true);
3732
}
3833

3934
[TestCleanup]
40-
public void Cleanup()
35+
public async Task Cleanup()
4136
{
42-
var tableExists = true;
43-
44-
//var status = AWSSDK_DotNet.IntegrationTests.Tests.DynamoDB.DynamoDBTests.GetStatus(tableName);
45-
//tableExists = (status != null);
46-
47-
var allTables = AWSSDK_DotNet.IntegrationTests.Tests.DynamoDB.DynamoDBTests.Client.ListTables().TableNames;
48-
tableExists = allTables.Contains(TABLENAME);
49-
50-
if (tableExists)
51-
DeleteTable(TABLENAME);
37+
var allTables = (await DynamoDBTests.Client.ListTablesAsync()).TableNames;
38+
39+
if (allTables.Contains(TABLENAME))
40+
{
41+
await DeleteTable(TABLENAME);
42+
}
5243

5344
if (allTables.Contains(testTableName))
54-
DeleteTable(testTableName);
45+
{
46+
await DeleteTable(testTableName);
47+
}
5548

5649
AWSConfigs.UseSdkCache = lastUseSdkCacheValue;
5750
SdkCache.Clear();
5851
}
5952

60-
53+
#if NETFRAMEWORK
6154
[TestMethod]
6255
[TestCategory("DynamoDBv2")]
6356
public void TestCache()
6457
{
65-
Func<string, TableDescription> creator = tn => client.DescribeTable(tn).Table;
58+
TableDescription creator(string tn) => client.DescribeTable(tn).Table;
6659

6760
var tableName = testTableName;
6861
var tableCache = SdkCache.GetCache<string, TableDescription>(client, DynamoDBTests.TableCacheIdentifier, StringComparer.Ordinal);
@@ -92,6 +85,7 @@ public void TestCache()
9285
Assert.AreEqual(1, tableCache.ItemCount);
9386
}
9487
}
88+
#endif
9589

9690
[TestMethod]
9791
[TestCategory("DynamoDBv2")]
@@ -103,20 +97,16 @@ public void MultipleClientsTest()
10397
ITable table;
10498
using (var nc = new AmazonDynamoDBClient())
10599
{
106-
#pragma warning disable CS0618 // Disable the warning for the deprecated DynamoDBContext constructors
107100
table = Table.LoadTable(nc, tableName);
108-
#pragma warning restore CS0618 // Re-enable the warning
109101
}
110102

111103
Table.ClearTableCache();
112-
#pragma warning disable CS0618 // Disable the warning for the deprecated DynamoDBContext constructors
113104
table = Table.LoadTable(client, tableName);
114-
#pragma warning restore CS0618 // Re-enable the warning
115105
}
116106

117107
[TestMethod]
118108
[TestCategory("DynamoDBv2")]
119-
public void ChangingTableTest()
109+
public async Task ChangingTableTest()
120110
{
121111
var item = new Document(new Dictionary<string, DynamoDBEntry>
122112
{
@@ -126,56 +116,50 @@ public void ChangingTableTest()
126116
});
127117
var tableCache = SdkCache.GetCache<string, TableDescription>(client, DynamoDBTests.TableCacheIdentifier, StringComparer.Ordinal);
128118

129-
CreateTable(TABLENAME, defaultKeys: true);
130-
#pragma warning disable CS0618 // Disable the warning for the deprecated DynamoDBContext constructors
119+
await CreateTable(TABLENAME, defaultKeys: true);
131120
var table = Table.LoadTable(client, TABLENAME);
132-
#pragma warning restore CS0618 // Re-enable the warning
133-
table.PutItem(item);
121+
await table.PutItemAsync(item);
134122

135123
using (var counter = new ServiceResponseCounter(client))
136124
{
137-
var doc = table.GetItem(42, "Floyd");
125+
var doc = await table.GetItemAsync(42, "Floyd");
138126
Assert.IsNotNull(doc);
139127
Assert.AreNotEqual(0, doc.Count);
140128
Assert.AreEqual(1, counter.ResponseCount);
141129
AssertExtensions.ExpectException(() => table.GetItem("Floyd", 42));
142130

143131
var oldTableDescription = tableCache.GetValue(TABLENAME, null);
144132

145-
DeleteTable(TABLENAME);
146-
CreateTable(TABLENAME, defaultKeys: false);
133+
await DeleteTable(TABLENAME);
134+
await CreateTable(TABLENAME, defaultKeys: false);
147135

148-
table.PutItem(item);
136+
await table.PutItemAsync(item);
149137
AssertExtensions.ExpectException(() => table.GetItem(42, "Yes"));
150138

151139
counter.Reset();
152140
Table.ClearTableCache();
153-
#pragma warning disable CS0618 // Disable the warning for the deprecated DynamoDBContext constructors
154141
table = Table.LoadTable(client, TABLENAME);
155-
#pragma warning restore CS0618 // Re-enable the warning
156-
doc = table.GetItem(42, "Yes");
142+
doc = await table.GetItemAsync(42, "Yes");
157143
Assert.IsNotNull(doc);
158144
Assert.AreNotEqual(0, doc.Count);
159145
Assert.AreEqual(2, counter.ResponseCount);
160146

161147
counter.Reset();
162148
Table.ClearTableCache();
163149
PutItem(tableCache, TABLENAME, oldTableDescription);
164-
#pragma warning disable CS0618 // Disable the warning for the deprecated DynamoDBContext constructors
165150
table = Table.LoadTable(client, TABLENAME);
166151
doc = tableCache.UseCache(TABLENAME,
167152
() => table.GetItem(42, "Yes"),
168153
() => table = Table.LoadTable(client, TABLENAME),
169154
shouldRetryForException: null);
170-
#pragma warning restore CS0618 // Re-enable the warning
171155

172156
Assert.IsNotNull(doc);
173157
Assert.AreNotEqual(0, doc.Count);
174158
Assert.AreEqual(2, counter.ResponseCount);
175159
}
176160
}
177161

178-
private void CreateTable(string name, bool defaultKeys)
162+
private async Task CreateTable(string name, bool defaultKeys)
179163
{
180164
var keySchema = new List<KeySchemaElement>
181165
{
@@ -205,20 +189,21 @@ private void CreateTable(string name, bool defaultKeys)
205189
}
206190
};
207191

208-
client.CreateTable(new CreateTableRequest
192+
await client.CreateTableAsync(new CreateTableRequest
209193
{
210194
TableName = name,
211195
KeySchema = keySchema,
212196
AttributeDefinitions = attributes,
213197
BillingMode = BillingMode.PAY_PER_REQUEST
214198
});
215199

216-
AWSSDK_DotNet.IntegrationTests.Tests.DynamoDB.DynamoDBTests.WaitForTableStatus(name, TableStatus.ACTIVE);
200+
await DynamoDBTests.WaitForTableStatus(name, TableStatus.ACTIVE);
217201
}
218-
private void DeleteTable(string name)
202+
203+
private async Task DeleteTable(string name)
219204
{
220-
client.DeleteTable(name);
221-
AWSSDK_DotNet.IntegrationTests.Tests.DynamoDB.DynamoDBTests.WaitForTableStatus(name, null);
205+
await client.DeleteTableAsync(name);
206+
await DynamoDBTests.WaitForTableStatus(name, null);
222207
}
223208

224209
private void PutItem<TKey,TValue>(ICache<TKey,TValue> cache, TKey key, TValue value)

0 commit comments

Comments
 (0)