Kinetica   C#   API  Version 7.2.3.1
AsyncBasicTests.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Threading.Tasks;
4 using Xunit;
6 using kinetica;
7 
9 {
14  [Trait("Category", "Integration")]
15  [Trait("Category", "Async")]
16  public class AsyncBasicTests
17  {
18  [Fact]
19  public async Task TestHasTypeAsync()
20  {
21  using var ctx = new TestContext("async_has_type");
22 
23  // Create a simple type
24  var typeDefinition = @"{
25  ""type"": ""record"",
26  ""name"": ""test_record"",
27  ""fields"": [
28  {""name"": ""id"", ""type"": ""int""},
29  {""name"": ""name"", ""type"": ""string""}
30  ]
31  }";
32 
33  var properties = new Dictionary<string, IList<string>>
34  {
35  { "id", new List<string> { "int", "primary_key" } }
36  };
37 
38  // Create the type asynchronously
39  var createResponse = await ctx.Kinetica.CreateTypeAsync(typeDefinition, "test_type_async", properties, new Dictionary<string, string>());
40  var typeId = createResponse.type_id;
41 
42  // Verify type exists using async has_type
43  var hasTypeResponse = await ctx.Kinetica.HasTypeAsync(typeId, new Dictionary<string, string>());
44  Assert.True(hasTypeResponse.type_exists, "Type should exist");
45 
46  // Verify non-existent type returns false
47  var hasTypeResponseNonexistent = await ctx.Kinetica.HasTypeAsync("nonexistent_type_async_12345", new Dictionary<string, string>());
48  Assert.False(hasTypeResponseNonexistent.type_exists, "Non-existent type should not exist");
49  }
50 
51  [Fact]
52  public async Task TestHasTableAsync()
53  {
54  using var ctx = new TestContext("async_has_table");
55 
56  // Create a type
57  var typeDefinition = @"{
58  ""type"": ""record"",
59  ""name"": ""test_record"",
60  ""fields"": [
61  {""name"": ""id"", ""type"": ""int""},
62  {""name"": ""value"", ""type"": ""double""}
63  ]
64  }";
65 
66  var properties = new Dictionary<string, IList<string>>
67  {
68  { "id", new List<string> { "int", "primary_key" } }
69  };
70 
71  var typeResponse = await ctx.Kinetica.CreateTypeAsync(typeDefinition, "async_has_table_type", properties, new Dictionary<string, string>());
72 
73  // Create a table asynchronously
74  var tableName = ctx.QualifiedTable("test_table_async");
75  await ctx.Kinetica.CreateTableAsync(tableName, typeResponse.type_id, new Dictionary<string, string>());
76 
77  // Verify table exists
78  var hasTableResponse = await ctx.Kinetica.HasTableAsync(tableName, new Dictionary<string, string>());
79  Assert.True(hasTableResponse.table_exists, "Table should exist");
80  Assert.Equal(tableName, hasTableResponse.table_name);
81 
82  // Verify non-existent table returns false
83  var nonexistentTable = ctx.QualifiedTable("nonexistent_table_async");
84  var hasTableResponseNonexistent = await ctx.Kinetica.HasTableAsync(nonexistentTable, new Dictionary<string, string>());
85  Assert.False(hasTableResponseNonexistent.table_exists, "Non-existent table should not exist");
86  }
87 
88  [Fact]
89  public async Task TestCreateAndClearTableAsync()
90  {
91  using var ctx = new TestContext("async_create_clear");
92 
93  // Create type
94  var typeDefinition = @"{
95  ""type"": ""record"",
96  ""name"": ""simple_record"",
97  ""fields"": [
98  {""name"": ""id"", ""type"": ""int""}
99  ]
100  }";
101 
102  var properties = new Dictionary<string, IList<string>>
103  {
104  { "id", new List<string> { "int", "primary_key" } }
105  };
106 
107  var typeResponse = await ctx.Kinetica.CreateTypeAsync(typeDefinition, "simple_type_async", properties, new Dictionary<string, string>());
108 
109  // Create table asynchronously
110  var tableName = ctx.QualifiedTable("simple_table_async");
111  var createTableResponse = await ctx.Kinetica.CreateTableAsync(tableName, typeResponse.type_id, new Dictionary<string, string>());
112  Assert.Equal(tableName, createTableResponse.table_name);
113 
114  // Verify table exists
115  var hasTableResponse = await ctx.Kinetica.HasTableAsync(tableName, new Dictionary<string, string>());
116  Assert.True(hasTableResponse.table_exists);
117 
118  // Clear table asynchronously
119  var clearResponse = await ctx.Kinetica.ClearTableAsync(tableName, null, new Dictionary<string, string>());
120  Assert.Equal(tableName, clearResponse.table_name);
121 
122  // Table should no longer exist after clear
123  var hasTableResponseAfter = await ctx.Kinetica.HasTableAsync(tableName, new Dictionary<string, string>());
124  Assert.False(hasTableResponseAfter.table_exists, "Table should not exist after clear");
125  }
126 
127  [Fact]
128  public async Task TestShowSystemPropertiesAsync()
129  {
130  using var ctx = new TestContext("async_show_system_props");
131 
132  // Call async show_system_properties
133  var response = await ctx.Kinetica.ShowSystemPropertiesAsync(new Dictionary<string, string>());
134 
135  Assert.NotNull(response);
136  Assert.NotNull(response.property_map);
137  Assert.True(response.property_map.Count > 0, "Should have system properties");
138 
139  // Verify some expected properties exist
140  Assert.True(response.property_map.ContainsKey("conf.version") ||
141  response.property_map.ContainsKey("version.gpudb_core_version"),
142  "Should contain version information");
143  }
144 
145  [Fact]
146  public async Task TestShowSystemStatusAsync()
147  {
148  using var ctx = new TestContext("async_show_system_status");
149 
150  // Call async show_system_status
151  var response = await ctx.Kinetica.ShowSystemStatusAsync(new Dictionary<string, string>());
152 
153  Assert.NotNull(response);
154  Assert.NotNull(response.status_map);
155  Assert.True(response.status_map.Count > 0, "Should have system status information");
156  }
157 
158  [Fact]
159  public async Task TestConcurrentAsyncOperations()
160  {
161  using var ctx = new TestContext("async_concurrent");
162 
163  // Create type
164  var typeDefinition = @"{
165  ""type"": ""record"",
166  ""name"": ""concurrent_record"",
167  ""fields"": [
168  {""name"": ""id"", ""type"": ""int""},
169  {""name"": ""data"", ""type"": ""string""}
170  ]
171  }";
172 
173  var properties = new Dictionary<string, IList<string>>
174  {
175  { "id", new List<string> { "int", "primary_key" } }
176  };
177 
178  var typeResponse = await ctx.Kinetica.CreateTypeAsync(typeDefinition, "concurrent_type", properties, new Dictionary<string, string>());
179 
180  // Create multiple tables concurrently
181  var tasks = new List<Task>();
182  var tableNames = new List<string>();
183 
184  for (int i = 0; i < 5; i++)
185  {
186  var tableName = ctx.QualifiedTable($"concurrent_table_{i}");
187  tableNames.Add(tableName);
188  tasks.Add(ctx.Kinetica.CreateTableAsync(tableName, typeResponse.type_id, new Dictionary<string, string>()));
189  }
190 
191  // Wait for all tables to be created
192  await Task.WhenAll(tasks);
193 
194  // Verify all tables exist concurrently
195  var verifyTasks = tableNames.Select(async tableName =>
196  {
197  var hasTableResponse = await ctx.Kinetica.HasTableAsync(tableName, new Dictionary<string, string>());
198  Assert.True(hasTableResponse.table_exists, $"Table {tableName} should exist");
199  });
200 
201  await Task.WhenAll(verifyTasks);
202  }
203 
204  [Fact]
205  public async Task TestAsyncWithCancellationToken()
206  {
207  using var ctx = new TestContext("async_cancellation");
208 
209  var cts = new System.Threading.CancellationTokenSource();
210 
211  // This should complete successfully
212  var response = await ctx.Kinetica.ShowSystemPropertiesAsync(new Dictionary<string, string>(), cts.Token);
213  Assert.NotNull(response);
214 
215  // Test cancellation (create token that's already cancelled)
216  var cancelledCts = new System.Threading.CancellationTokenSource();
217  cancelledCts.Cancel();
218 
219  // The transport layer wraps cancellation in KineticaException
220  var exception = await Assert.ThrowsAsync<KineticaException>(async () =>
221  {
222  await ctx.Kinetica.ShowSystemPropertiesAsync(new Dictionary<string, string>(), cancelledCts.Token);
223  });
224 
225  // Verify the exception is related to cancellation/timeout
226  Assert.Contains("timed out", exception.Message.ToLower());
227  }
228  }
229 }
Async integration tests for core API functionality.
Test context that manages schema and cleanup for integration tests.
Definition: TestContext.cs:11