Skip to content
This repository was archived by the owner on Jul 15, 2023. It is now read-only.

Commit 7d49e3b

Browse files
author
J Wyman
authored
Merge pull request #740 from whoisj/fix-flaky-tests
alm: fix flaky tests
2 parents 7c7eab4 + 686090f commit 7d49e3b

File tree

2 files changed

+55
-35
lines changed

2 files changed

+55
-35
lines changed

Microsoft.Alm.Authentication/Test/CredentialTests.cs

Lines changed: 54 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Threading.Tasks;
43
using Xunit;
54

@@ -9,11 +8,11 @@ public class CredentialTests
98
{
109
private const string Namespace = "test";
1110

12-
public static object[][] CredentialData
11+
public static object[ ][ ] CredentialData
1312
{
1413
get
1514
{
16-
List<object[]> data = new List<object[]>()
15+
return new object[ ][ ]
1716
{
1817
new object[] { false, "http://dummy.url/for/testing", "username", "password", false },
1918
new object[] { false, "http://dummy.url/for/testing?with=params", "username", "password", false },
@@ -32,17 +31,30 @@ public static object[][] CredentialData
3231
new object[] { true, "http://dummy.url/for/testing", "null_passwords_are_legal", null, false },
3332
new object[] { true, "http://dummy.url/for/testing", "blank_passwords_are_legal", "", false },
3433
new object[] { true, "http://dummy.url:999/for/testing", "username", "password", false },
35-
};
3634

37-
return data.ToArray();
35+
new object[] { false, "http://[email protected]/for/testing", "username", "password", false },
36+
new object[] { false, "http://[email protected]/for/testing?with=params", "username", "password", false },
37+
new object[] { false, "http://[email protected]/for/testing", null, "null_usernames_are_illegal", true },
38+
new object[] { false, "http://[email protected]/for/testing", "", "blank_usernames_are_legal", false },
39+
new object[] { false, "http://[email protected]/for/testing", "null_passwords_are_legal", null, false },
40+
new object[] { false, "http://[email protected]/for/testing", "blank_passwords_are_legal", "", false },
41+
new object[] { false, "http://[email protected]:999/for/testing", "username", "password", false },
42+
43+
new object[] { true, "http://[email protected]/for/testing", "username", "password", false },
44+
new object[] { true, "http://[email protected]/for/testing?with=params", "username", "password", false },
45+
new object[] { true, "http://[email protected]/for/testing", null, "null_usernames_are_illegal", true },
46+
new object[] { true, "http://[email protected]/for/testing", "", "blank_usernames_are_legal", false },
47+
new object[] { true, "http://[email protected]/for/testing", "null_passwords_are_legal", null, false },
48+
new object[] { true, "http://[email protected]/for/testing", "blank_passwords_are_legal", "", false },
49+
new object[] { true, "http://[email protected]:999/for/testing", "username", "password", false },
50+
};
3851
}
3952
}
4053

41-
[Theory]
42-
[MemberData(nameof(CredentialData), DisableDiscoveryEnumeration = true)]
43-
public void Credential_WriteDelete(bool useCache, string url, string username, string password, bool throws)
54+
[Theory, MemberData(nameof(CredentialData), DisableDiscoveryEnumeration = false)]
55+
public async Task Credential_WriteDelete(bool useCache, string url, string username, string password, bool throws)
4456
{
45-
var task = Task.Run(async () =>
57+
try
4658
{
4759
var uri = new TargetUri(url);
4860
var writeCreds = new Credential(username, password);
@@ -60,35 +72,45 @@ public void Credential_WriteDelete(bool useCache, string url, string username, s
6072

6173
await credentialStore.DeleteCredentials(uri);
6274

63-
Assert.Null(readCreds = await credentialStore.ReadCredentials(uri));
64-
});
75+
readCreds = await credentialStore.ReadCredentials(uri);
76+
Assert.Null(readCreds);
77+
}
78+
catch (ArgumentNullException) when (throws)
79+
{
80+
/* We expected the exception */
81+
}
82+
}
6583

66-
if (throws)
84+
[Theory, MemberData(nameof(CredentialData), DisableDiscoveryEnumeration = false)]
85+
public async Task Credential_WriteRead(bool useCache, string url, string username, string password, bool throws)
86+
{
87+
try
6788
{
68-
Assert.Throws<ArgumentNullException>(() =>
69-
{
70-
try
71-
{
72-
task.Wait();
73-
}
74-
catch (System.AggregateException exception)
75-
{
76-
exception = exception.Flatten();
77-
throw exception.InnerException;
78-
}
79-
});
89+
var uri = new TargetUri(url);
90+
var writeCreds = new Credential(username, password);
91+
var credentialStore = useCache
92+
? new SecretCache(RuntimeContext.Default, "test", Secret.UriToName) as ICredentialStore
93+
: new SecretStore(RuntimeContext.Default, "test", null, null, Secret.UriToName) as ICredentialStore;
94+
Credential readCreds = null;
95+
96+
await credentialStore.WriteCredentials(uri, writeCreds);
97+
98+
readCreds = await credentialStore.ReadCredentials(uri);
99+
Assert.NotNull(readCreds);
100+
Assert.Equal(writeCreds.Password, readCreds.Password);
101+
Assert.Equal(writeCreds.Username, readCreds.Username);
80102
}
81-
else
103+
catch (ArgumentNullException) when (throws)
82104
{
83-
task.Wait();
105+
/* We expected the exception */
84106
}
85107
}
86108

87-
public static object[][] UriToNameData
109+
public static object[ ][ ] UriToNameData
88110
{
89111
get
90112
{
91-
return new object[][]
113+
return new object[ ][ ]
92114
{
93115
new object[] { "https://microsoft.visualstudio.com", null },
94116
new object[] { "https://www.github.com", null },
@@ -111,8 +133,7 @@ public static object[][] UriToNameData
111133
}
112134
}
113135

114-
[Theory]
115-
[MemberData(nameof(UriToNameData), DisableDiscoveryEnumeration = true)]
136+
[Theory, MemberData(nameof(UriToNameData), DisableDiscoveryEnumeration = true)]
116137
public void UriToNameTest(string original, string expected)
117138
{
118139
var uri = new Uri(original);
@@ -123,11 +144,11 @@ public void UriToNameTest(string original, string expected)
123144
Assert.Equal(expected, actual, StringComparer.Ordinal);
124145
}
125146

126-
public static object[][] UriToIdentityNameData
147+
public static object[ ][ ] UriToIdentityNameData
127148
{
128149
get
129150
{
130-
return new object[][]
151+
return new object[ ][ ]
131152
{
132153
new object[] { "https://microsoft.visualstudio.com", null },
133154
new object[] { "https://www.github.com", null },
@@ -150,8 +171,7 @@ public static object[][] UriToIdentityNameData
150171
}
151172
}
152173

153-
[Theory]
154-
[MemberData(nameof(UriToIdentityNameData), DisableDiscoveryEnumeration = true)]
174+
[Theory, MemberData(nameof(UriToIdentityNameData), DisableDiscoveryEnumeration = true)]
155175
public void UriToIdentityNameTest(string original, string expected)
156176
{
157177
var uri = new Uri(original);

Microsoft.Alm.Authentication/Test/TokenTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public static object[][] TokenStoreData
3131
}
3232

3333
[Theory]
34-
[MemberData(nameof(TokenStoreData), DisableDiscoveryEnumeration = true)]
34+
[MemberData(nameof(TokenStoreData), DisableDiscoveryEnumeration = false)]
3535
public async Task Token_WriteDelete(bool useCache, string secretName, string url, string token)
3636
{
3737
var tokenStore = useCache

0 commit comments

Comments
 (0)