-
Notifications
You must be signed in to change notification settings - Fork 235
Expand file tree
/
Copy pathRecordSession.cs
More file actions
179 lines (156 loc) · 5.24 KB
/
RecordSession.cs
File metadata and controls
179 lines (156 loc) · 5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
namespace Azure.Sdk.Tools.TestProxy.Common
{
public class RecordSession
{
internal const string DateTimeOffsetNowVariableKey = "DateTimeOffsetNow";
public List<RecordEntry> Entries { get; } = new List<RecordEntry>();
public SortedDictionary<string, string> Variables { get; } = new SortedDictionary<string, string>(StringComparer.OrdinalIgnoreCase);
//Used only for deserializing track 1 session record files
public Dictionary<string, Queue<string>> Names { get; set; } = new Dictionary<string, Queue<string>>();
public SemaphoreSlim EntryLock { get; set; } = new SemaphoreSlim(1);
public void Serialize(Utf8JsonWriter jsonWriter)
{
jsonWriter.WriteStartObject();
jsonWriter.WriteStartArray(nameof(Entries));
foreach (RecordEntry record in Entries)
{
record.Serialize(jsonWriter);
}
jsonWriter.WriteEndArray();
jsonWriter.WriteStartObject(nameof(Variables));
foreach (KeyValuePair<string, string> variable in Variables)
{
jsonWriter.WriteString(variable.Key, variable.Value);
}
jsonWriter.WriteEndObject();
jsonWriter.WriteEndObject();
}
public static RecordSession Deserialize(JsonElement element)
{
var session = new RecordSession();
if (element.TryGetProperty(nameof(Entries), out JsonElement property))
{
foreach (JsonElement item in property.EnumerateArray())
{
session.Entries.Add(RecordEntry.Deserialize(item));
}
}
if (element.TryGetProperty(nameof(Variables), out property))
{
foreach (JsonProperty item in property.EnumerateObject())
{
session.Variables[item.Name] = item.Value.GetString();
}
}
if (element.TryGetProperty(nameof(Names), out property))
{
foreach (JsonProperty item in property.EnumerateObject())
{
var queue = new Queue<string>();
foreach (JsonElement subItem in item.Value.EnumerateArray())
{
queue.Enqueue(subItem.GetString());
}
session.Names[item.Name] = queue;
}
}
return session;
}
public async Task Record(RecordEntry entry, bool shouldLock = true)
{
if (shouldLock)
{
await EntryLock.WaitAsync();
}
try
{
Entries.Add(entry);
}
finally
{
if (shouldLock)
{
EntryLock.Release();
}
}
}
public RecordEntry Lookup(RecordEntry requestEntry, RecordMatcher matcher, IEnumerable<RecordedTestSanitizer> sanitizers, bool remove = true, string sessionId = null)
{
foreach (RecordedTestSanitizer sanitizer in sanitizers)
{
sanitizer.Sanitize(requestEntry);
}
RecordEntry entry = matcher.FindMatch(requestEntry, Entries);
if (remove)
{
Entries.Remove(entry);
DebugLogger.LogDebug($"We successfully matched and popped request URI {entry.RequestUri} for recordingId {sessionId??"Unknown"}");
}
return entry;
}
public async Task Remove(RecordEntry entry, bool shouldLock = true)
{
if (shouldLock)
{
await EntryLock.WaitAsync();
}
try
{
Entries.Remove(entry);
}
finally
{
if (shouldLock)
{
EntryLock.Release();
}
}
}
public async Task Sanitize(RecordedTestSanitizer sanitizer, bool shouldLock = true)
{
if (shouldLock)
{
await EntryLock.WaitAsync();
}
try
{
sanitizer.Sanitize(this);
}
finally
{
if (shouldLock)
{
EntryLock.Release();
}
}
}
public async Task Sanitize(IEnumerable<RecordedTestSanitizer> sanitizers, bool shouldLock = true)
{
if (shouldLock)
{
await EntryLock.WaitAsync();
}
try
{
foreach (var sanitizer in sanitizers)
{
sanitizer.Sanitize(this);
}
}
finally
{
if (shouldLock)
{
EntryLock.Release();
}
}
}
}
}