Skip to content

Commit

Permalink
chore: use client test fixture to speedup tests (#81)
Browse files Browse the repository at this point in the history
This PR introduces a client test fixture. The previous tests
instantiated the client in test class constructors. That is, we used
this pattern:

```csharp
public class MyTests
{
	public MyTests()
	{
		// Instantiate some things
	}

	public void Test_Something()
	{
		// tests
	}

	public void Test_Otherthing()
	{
		// tests
	}
}
```

Data instantiated in the constructor will be setup and torn down
for *each* test method, not just for the class as a whole. This is not
the same thing `setUp` from jUnit.

In Fixtures.cs we create a client fixture and register it to be
available across all test classes. Other tests modified appropriately.
  • Loading branch information
malandis authored Jul 20, 2022
1 parent 10bcf25 commit 3fbce9d
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 132 deletions.
46 changes: 46 additions & 0 deletions MomentoIntegrationTest/Fixtures.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
namespace MomentoIntegrationTest
{
/// <summary>
/// A cache client fixture.
/// Use this when not testing client-building edge cases:
/// re-using the client drops overall integration test time down ~5X.
/// </summary>
public class SimpleCacheClientFixture : IDisposable
{
public SimpleCacheClient Client { get; private set; }
public string AuthToken { get; private set; }

public const string CacheName = "client-sdk-csharp";
public const uint DefaultTtlSeconds = 10;

public SimpleCacheClientFixture()
{
AuthToken = Environment.GetEnvironmentVariable("TEST_AUTH_TOKEN") ??
throw new NullReferenceException("TEST_AUTH_TOKEN environment variable must be set.");
Client = new(AuthToken, defaultTtlSeconds: DefaultTtlSeconds);

try
{
Client.CreateCache(CacheName);
}
catch (AlreadyExistsException)
{
}
}

public void Dispose()
{
Client.DeleteCache(CacheName);
Client.Dispose();
}
}

/// <summary>
/// Register the fixture in xUnit.
/// </summary>
[CollectionDefinition("SimpleCacheClient")]
public class SimpleCacheClientCollection : ICollectionFixture<SimpleCacheClientFixture>
{

}
}
28 changes: 12 additions & 16 deletions MomentoIntegrationTest/SimpleCacheControlTest.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
using System;
using System.Linq;
using Xunit;
using MomentoSdk;
using MomentoSdk.Exceptions;
using MomentoSdk.Responses;
using System.Linq;
using System.Collections.Generic;

namespace MomentoIntegrationTest
{
[Collection("SimpleCacheClient")]
public class SimpleCacheControlTest
{
private string authKey = Environment.GetEnvironmentVariable("TEST_AUTH_TOKEN") ??
throw new NullReferenceException("TEST_AUTH_TOKEN environment variable must be set.");
private SimpleCacheClient client;
private string authToken;

public SimpleCacheControlTest(SimpleCacheClientFixture fixture)
{
client = fixture.Client;
authToken = fixture.AuthToken;
}

[Fact]
public void SimpleCacheClientConstructor_BadRequestTimeout_ThrowsException()
{
Assert.Throws<InvalidArgumentException>(() => new SimpleCacheClient(authKey, defaultTtlSeconds: 10, dataClientOperationTimeoutMilliseconds: 0));
Assert.Throws<InvalidArgumentException>(() => new SimpleCacheClient(authToken, defaultTtlSeconds: 10, dataClientOperationTimeoutMilliseconds: 0));
}

[Fact]
Expand All @@ -34,21 +36,18 @@ public void SimpleCacheClientConstructor_NullJWT_InvalidJwtException()
[Fact]
public void DeleteCache_NullCache_ArgumentNullException()
{
SimpleCacheClient client = new SimpleCacheClient(authKey, defaultTtlSeconds: 10);
Assert.Throws<ArgumentNullException>(() => client.DeleteCache(null!));
}

[Fact]
public void DeleteCache_CacheDoesntExist_NotFoundException()
{
SimpleCacheClient client = new SimpleCacheClient(authKey, defaultTtlSeconds: 10);
Assert.Throws<NotFoundException>(() => client.DeleteCache("non existant cache"));
Assert.Throws<NotFoundException>(() => client.DeleteCache("non-existent cache"));
}

[Fact]
public void CreateCache_NullCache_ArgumentNullException()
{
SimpleCacheClient client = new SimpleCacheClient(authKey, defaultTtlSeconds: 10);
Assert.Throws<ArgumentNullException>(() => client.CreateCache(null!));
}

Expand All @@ -57,7 +56,6 @@ public void CreateCache_NullCache_ArgumentNullException()
public void ListCaches_OneCache_HappyPath()
{
// Create cache
SimpleCacheClient client = new SimpleCacheClient(authKey, defaultTtlSeconds: 10);
string cacheName = Guid.NewGuid().ToString();
client.CreateCache(cacheName);

Expand All @@ -77,7 +75,6 @@ public void ListCaches_OneCache_HappyPath()
public void ListCaches_Iteration_HappyPath()
{
// Create caches
SimpleCacheClient client = new SimpleCacheClient(authKey, defaultTtlSeconds: 10);
List<String> cacheNames = new List<String>();

// TODO: increase limit after pagination is enabled
Expand Down Expand Up @@ -119,7 +116,6 @@ public void ListCaches_Iteration_HappyPath()
public void ListCaches_BadNextToken_NoException()
{
// A bad next token does not throw an exception
SimpleCacheClient client = new SimpleCacheClient(authKey, defaultTtlSeconds: 10);
client.ListCaches(nextPageToken: "hello world");
}
}
Expand Down
Loading

0 comments on commit 3fbce9d

Please sign in to comment.