Skip to content

Commit 93007be

Browse files
ebozdumanErsan Bozduman
and
Ersan Bozduman
authored
Adds missing parts of the fix in PR #1110 (#1130)
* adds missing parts of the fix in PR #1110 * increases subscription wait time * moves wait to its proper place * lint changes * incorporates review comment * lint changes * unreported regitlint changes --------- Co-authored-by: Ersan Bozduman <[email protected]>
1 parent a2589e1 commit 93007be

File tree

6 files changed

+61
-103
lines changed

6 files changed

+61
-103
lines changed

Minio.Examples/Cases/ListenNotifications.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static void Run(IMinioClient minio,
3232
Console.WriteLine();
3333
events ??= new List<EventType> { EventType.BucketCreatedAll };
3434
var args = new ListenBucketNotificationsArgs().WithEvents(events);
35-
var observable = minio.ListenNotificationsAsync(events);
35+
var observable = minio.ListenNotifications(args);
3636

3737
subscription = observable.Subscribe(
3838
notification => Console.WriteLine($"Notification: {notification.Json}"),

Minio.Functional.Tests/FunctionalTest.cs

+55-96
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public static class FunctionalTest
8484
"IObservable<MinioNotificationRaw> ListenBucketNotificationsAsync(ListenBucketNotificationsArgs args, CancellationToken cancellationToken = default(CancellationToken))";
8585

8686
private const string listenNotificationsSignature =
87-
"IObservable<MinioNotificationRaw> ListenNotificationsAsync(IList<EventType> events, CancellationToken cancellationToken = default(CancellationToken))";
87+
"IObservable<MinioNotificationRaw> ListenNotifications(ListenBucketNotificationsArgs args, CancellationToken cancellationToken = default(CancellationToken))";
8888

8989
private const string copyObjectSignature =
9090
"Task<CopyObjectResult> CopyObjectAsync(CopyObjectArgs args, CancellationToken cancellationToken = default(CancellationToken))";
@@ -1041,13 +1041,11 @@ internal static async Task<bool> CreateBucket_Tester(IMinioClient minio, string
10411041
{
10421042
// Create a new bucket
10431043
await minio.MakeBucketAsync(new MakeBucketArgs().WithBucket(bucketName)).ConfigureAwait(false);
1044+
await Task.Delay(800).ConfigureAwait(false);
10441045

10451046
// Verify the bucket exists
1046-
var bucketExists = await minio.BucketExistsAsync(new BucketExistsArgs().WithBucket(bucketName))
1047+
return await minio.BucketExistsAsync(new BucketExistsArgs().WithBucket(bucketName))
10471048
.ConfigureAwait(false);
1048-
Assert.IsTrue(bucketExists, $"Bucket {bucketName} was not created successfully.");
1049-
1050-
return bucketExists;
10511049
}
10521050

10531051
internal static async Task StatObject_Test1(IMinioClient minio)
@@ -1282,7 +1280,7 @@ internal static async Task RemoveObjects_Test3(IMinioClient minio)
12821280
.WithBucket(bucketName)
12831281
.WithObjectsVersions(objVersions);
12841282

1285-
await minio.RemoveObjectsAsync(removeObjectsArgs).ConfigureAwait(false);
1283+
_ = await minio.RemoveObjectsAsync(removeObjectsArgs).ConfigureAwait(false);
12861284

12871285
await TearDown(minio, bucketName).ConfigureAwait(false);
12881286

@@ -2639,7 +2637,7 @@ await ListObjects_Test(minio, bucketName, singleObjectName, 1, headers: extractH
26392637

26402638
#region Global Notifications
26412639

2642-
internal static async Task ListenNotificationsAsync_Test1(IMinioClient minio)
2640+
internal static async Task ListenNotifications_Test1(IMinioClient minio)
26432641
{
26442642
var startTime = DateTime.Now;
26452643
var bucketName = GetRandomName(15);
@@ -2648,61 +2646,54 @@ internal static async Task ListenNotificationsAsync_Test1(IMinioClient minio)
26482646
try
26492647
{
26502648
var received = new List<MinioNotificationRaw>();
2651-
26522649
var eventsList = new List<EventType> { EventType.BucketCreatedAll };
26532650

2654-
var events = minio.ListenNotificationsAsync(eventsList);
2655-
var subscription = events.Subscribe(
2656-
received.Add,
2657-
ex => Console.WriteLine($"OnError: {ex}"),
2658-
() => Console.WriteLine("Stopped listening for bucket notifications\n"));
2659-
2660-
// Ensure the subscription is established
2661-
await Task.Delay(1000).ConfigureAwait(false);
2662-
2663-
// Trigger the event by creating a new bucket
2664-
var isBucketCreated1 = await CreateBucket_Tester(minio, bucketName).ConfigureAwait(false);
2665-
2651+
// No need to define a new "ListenNotificationArgs"
2652+
// "ListenBucketNotificationsArgs" is good here
2653+
var listenArgs = new ListenBucketNotificationsArgs()
2654+
.WithEvents(eventsList);
2655+
var events = minio.ListenNotifications(listenArgs);
26662656
var eventDetected = false;
2657+
using (var subscription = events.Subscribe(
2658+
received.Add,
2659+
_ => { },
2660+
() => { }))
2661+
{
2662+
await Task.Delay(200).ConfigureAwait(false);
2663+
// Trigger the event by creating a new bucket
2664+
_ = await CreateBucket_Tester(minio, bucketName).ConfigureAwait(false);
2665+
}
2666+
26672667
for (var attempt = 0; attempt < 20; attempt++)
2668-
{
2669-
if (received.Count > 0)
2668+
// Check if there is a caught event
2669+
if (received.Count == 1)
26702670
{
26712671
var notification = JsonSerializer.Deserialize<MinioNotification>(received[0].Json);
26722672

26732673
if (notification.Records is not null)
26742674
{
26752675
Assert.AreEqual(1, notification.Records.Count);
2676+
Assert.IsTrue(notification.Records[0].EventName
2677+
.Contains("s3:BucketCreated:*", StringComparison.OrdinalIgnoreCase));
26762678
eventDetected = true;
26772679
break;
26782680
}
26792681
}
26802682

2681-
await Task.Delay(500).ConfigureAwait(false); // Delay between attempts
2682-
}
2683-
2684-
subscription.Dispose();
2685-
if (!eventDetected)
2686-
throw new UnexpectedMinioException("Failed to detect the expected bucket notification event.");
2687-
2688-
new MintLogger(nameof(ListenNotificationsAsync_Test1),
2689-
listenNotificationsSignature,
2690-
"Tests whether ListenNotifications passes",
2691-
TestStatus.PASS, DateTime.Now - startTime, args: args).Log();
2692-
}
2693-
catch (NotImplementedException ex)
2694-
{
2695-
new MintLogger(nameof(ListenNotificationsAsync_Test1),
2696-
listenNotificationsSignature,
2697-
"Tests whether ListenNotifications passes",
2698-
TestStatus.NA, DateTime.Now - startTime, ex.Message,
2699-
ex.ToString(), args: args).Log();
2683+
if (eventDetected)
2684+
new MintLogger(nameof(ListenNotifications_Test1),
2685+
listenNotificationsSignature,
2686+
"Tests whether ListenNotifications notifies user about \"BucketCreatedAll\" event",
2687+
TestStatus.PASS, DateTime.Now - startTime, args: args).Log();
2688+
else
2689+
throw new UnexpectedMinioException(
2690+
"Failed to detect the expected notification event, \"BucketCreatedAll\".");
27002691
}
27012692
catch (Exception ex)
27022693
{
2703-
new MintLogger(nameof(ListenNotificationsAsync_Test1),
2694+
new MintLogger(nameof(ListenNotifications_Test1),
27042695
listenNotificationsSignature,
2705-
"Tests whether ListenNotifications passes",
2696+
"Tests whether ListenNotifications notifies user about \"BucketCreatedAll\" event",
27062697
TestStatus.FAIL, DateTime.Now - startTime, ex.Message,
27072698
ex.ToString(), args: args).Log();
27082699
throw;
@@ -2738,57 +2729,26 @@ internal static async Task ListenBucketNotificationsAsync_Test1(IMinioClient min
27382729
var received = new List<MinioNotificationRaw>();
27392730

27402731
var eventsList = new List<EventType> { EventType.ObjectCreatedAll };
2732+
var eventDetected = false;
27412733

27422734
var listenArgs = new ListenBucketNotificationsArgs()
27432735
.WithBucket(bucketName)
27442736
.WithEvents(eventsList);
27452737
var events = minio.ListenBucketNotificationsAsync(listenArgs);
2746-
var subscription = events.Subscribe(
2747-
received.Add,
2748-
ex => { },
2749-
() => { }
2750-
);
2751-
2752-
2753-
_ = await PutObject_Tester(minio, bucketName, objectName, null, contentType,
2754-
0, null, rsg.GenerateStreamFromSeed(1 * KB)).ConfigureAwait(false);
2738+
using (var subscription = events.Subscribe(
2739+
received.Add,
2740+
_ => { },
2741+
() => { }))
2742+
{
2743+
_ = await PutObject_Tester(minio, bucketName, objectName, null, contentType,
2744+
0, null, rsg.GenerateStreamFromSeed(1 * KB)).ConfigureAwait(false);
2745+
}
27552746

2756-
// wait for notifications
2757-
var eventDetected = false;
2758-
for (var attempt = 0; attempt < 10; attempt++)
2759-
if (received.Count > 0)
2747+
for (var attempt = 0; attempt < 20; attempt++)
2748+
// Check if there is a caught event
2749+
if (received.Count == 1)
27602750
{
2761-
// Check if there is any unexpected error returned
2762-
// and captured in the receivedJson list, like
2763-
// "NotImplemented" api error. If so, we throw an exception
2764-
// and skip running this test
2765-
if (received.Count > 1 &&
2766-
received[1].Json.StartsWith("<Error><Code>", StringComparison.OrdinalIgnoreCase))
2767-
{
2768-
// Although the attribute is called "json",
2769-
// returned data in list "received" is in xml
2770-
// format and it is an error.Here, we convert xml
2771-
// into json format.
2772-
var receivedJson = XmlStrToJsonStr(received[1].Json);
2773-
2774-
// Cleanup the "Error" key encapsulating "receivedJson"
2775-
// data. This is required to match and convert json data
2776-
// "receivedJson" into class "ErrorResponse"
2777-
var len = "{'Error':".Length;
2778-
var trimmedFront = receivedJson[len..];
2779-
var trimmedFull = trimmedFront[..^1];
2780-
2781-
var err = JsonSerializer.Deserialize<ErrorResponse>(trimmedFull);
2782-
2783-
Exception ex = new UnexpectedMinioException(err.Message);
2784-
if (string.Equals(err.Code, "NotImplemented", StringComparison.OrdinalIgnoreCase))
2785-
ex = new NotImplementedException(err.Message);
2786-
await TearDown(minio, bucketName).ConfigureAwait(false);
2787-
throw ex;
2788-
}
2789-
27902751
var notification = JsonSerializer.Deserialize<MinioNotification>(received[0].Json);
2791-
27922752
if (notification.Records is not null)
27932753
{
27942754
Assert.AreEqual(1, notification.Records.Count);
@@ -2804,20 +2764,19 @@ internal static async Task ListenBucketNotificationsAsync_Test1(IMinioClient min
28042764
}
28052765
}
28062766

2807-
// subscription.Dispose();
2808-
if (!eventDetected)
2767+
if (eventDetected)
2768+
new MintLogger(nameof(ListenBucketNotificationsAsync_Test1),
2769+
listenBucketNotificationsSignature,
2770+
"Tests whether ListenBucketNotifications passes for small object",
2771+
TestStatus.PASS, DateTime.Now - startTime, args: args).Log();
2772+
else
28092773
throw new UnexpectedMinioException("Failed to detect the expected bucket notification event.");
2810-
2811-
new MintLogger(nameof(ListenBucketNotificationsAsync_Test1),
2812-
listenBucketNotificationsSignature,
2813-
"Tests whether ListenBucketNotifications passes for small object",
2814-
TestStatus.PASS, DateTime.Now - startTime, args: args).Log();
28152774
}
28162775
catch (NotImplementedException ex)
28172776
{
28182777
new MintLogger(nameof(ListenBucketNotificationsAsync_Test1),
28192778
listenBucketNotificationsSignature,
2820-
"Tests whether ListenBucketNotifications passes for small object",
2779+
"Tests whether ListenBucketNotifications generates notification for a newly created object.",
28212780
TestStatus.NA, DateTime.Now - startTime, ex.Message,
28222781
ex.ToString(), args: args).Log();
28232782
}
@@ -2841,14 +2800,14 @@ static bool isAWS(string endPoint)
28412800
// This is a PASS
28422801
new MintLogger(nameof(ListenBucketNotificationsAsync_Test1),
28432802
listenBucketNotificationsSignature,
2844-
"Tests whether ListenBucketNotifications passes for small object",
2803+
"Tests whether ListenBucketNotifications generates notification for a newly created object.",
28452804
TestStatus.PASS, DateTime.Now - startTime, args: args).Log();
28462805
}
28472806
else
28482807
{
28492808
new MintLogger(nameof(ListenBucketNotificationsAsync_Test1),
28502809
listenBucketNotificationsSignature,
2851-
"Tests whether ListenBucketNotifications passes for small object",
2810+
"Tests whether ListenBucketNotifications generates notification for a newly created object.",
28522811
TestStatus.FAIL, DateTime.Now - startTime, ex.Message,
28532812
ex.ToString(), args: args).Log();
28542813
throw;

Minio.Functional.Tests/Program.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public static async Task Main(string[] args)
108108
ConcurrentBag<Task> functionalTestTasks = new();
109109

110110
// Global Notification
111-
await FunctionalTest.ListenNotificationsAsync_Test1(minioClient).ConfigureAwait(false);
111+
await FunctionalTest.ListenNotifications_Test1(minioClient).ConfigureAwait(false);
112112

113113
// Try catch as 'finally' section needs to run in the Functional Tests
114114
// Bucket notification is a minio specific feature.

Minio/ApiEndpoints/BucketOperations.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -762,13 +762,12 @@ await this.ExecuteTaskAsync(ResponseErrorHandlers, requestMessageBuilder,
762762
/// <summary>
763763
/// Subscribes to global change notifications (a Minio-only extension)
764764
/// </summary>
765-
/// <param name="events">Events to listen for</param>
765+
/// <param name="args">ListenBucketNotificationsArgs to listen events</param>
766766
/// <param name="cancellationToken">Optional cancellation token to cancel the operation</param>
767767
/// <returns>An observable of JSON-based notification events</returns>
768-
public IObservable<MinioNotificationRaw> ListenNotificationsAsync(IList<EventType> events,
768+
public IObservable<MinioNotificationRaw> ListenNotifications(ListenBucketNotificationsArgs args,
769769
CancellationToken cancellationToken = default)
770770
{
771-
var args = new ListenBucketNotificationsArgs().WithEvents(events);
772771
return ListenBucketNotificationsAsync(args, cancellationToken);
773772
}
774773

Minio/ApiEndpoints/IBucketOperations.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ Task<ReplicationConfiguration> GetBucketReplicationAsync(GetBucketReplicationArg
379379

380380
Task<string> GetPolicyAsync(GetPolicyArgs args, CancellationToken cancellationToken = default);
381381

382-
IObservable<MinioNotificationRaw> ListenNotificationsAsync(IList<EventType> events,
382+
IObservable<MinioNotificationRaw> ListenNotifications(ListenBucketNotificationsArgs args,
383383
CancellationToken cancellationToken = default);
384384

385385
IObservable<MinioNotificationRaw> ListenBucketNotificationsAsync(string bucketName, IList<EventType> events,

Minio/Minio.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
<PackageReference Include="System.Net.Http" Version="4.3.4" />
2626
<PackageReference Include="System.Net.Primitives" Version="4.3.1" />
2727
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
28-
<PackageReference Include="System.Text.Json" Version="8.0.3" />
28+
<PackageReference Include="System.Text.Json" Version="8.0.4" />
2929
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.4" />
3030
<PackageReference Include="System.Security.Cryptography.Algorithms" Version="4.3.1" />
3131
</ItemGroup>

0 commit comments

Comments
 (0)