|
1 | 1 | // Copyright (c) .NET Foundation. All rights reserved.
|
2 | 2 | // Licensed under the MIT License. See License.txt in the project root for license information.
|
3 | 3 |
|
| 4 | +using System.Collections; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Collections.ObjectModel; |
4 | 7 | using System.IO;
|
5 | 8 | using System.Net;
|
| 9 | +using System.Threading; |
6 | 10 | using System.Threading.Tasks;
|
7 | 11 | using Microsoft.AspNetCore.Authentication;
|
8 | 12 | using Microsoft.AspNetCore.Authorization;
|
9 | 13 | using Microsoft.AspNetCore.Mvc;
|
10 | 14 | using Microsoft.Azure.WebJobs.Host.Scale;
|
| 15 | +using Microsoft.Azure.WebJobs.Script.Description; |
11 | 16 | using Microsoft.Azure.WebJobs.Script.ExtensionBundle;
|
12 | 17 | using Microsoft.Azure.WebJobs.Script.Scale;
|
13 | 18 | using Microsoft.Azure.WebJobs.Script.WebHost.Controllers;
|
@@ -166,5 +171,111 @@ public async Task GetScaleStatus_RuntimeScaleModeNotEnabled_ReturnsBadRequest()
|
166 | 171 | Assert.Equal(HttpStatusCode.BadRequest, (HttpStatusCode)result.StatusCode);
|
167 | 172 | Assert.Equal("Runtime scale monitoring is not enabled.", result.Value);
|
168 | 173 | }
|
| 174 | + |
| 175 | + [Theory] |
| 176 | + [ClassData(typeof(WarmupTestData))] |
| 177 | + public async Task TestWarmupEndpoint_Success(FunctionDescriptor[] functions, bool warmupCalled) |
| 178 | + { |
| 179 | + var triggerParamName = "triggerParam"; |
| 180 | + var scriptHostMock = new Mock<IScriptJobHost>(); |
| 181 | + bool functionInvoked = false; |
| 182 | + |
| 183 | + scriptHostMock.Setup(p => p.CallAsync(It.IsAny<string>(), It.IsAny<IDictionary<string, object>>(), CancellationToken.None)) |
| 184 | + .Callback<string, IDictionary<string, object>, CancellationToken>((name, args, token) => |
| 185 | + { |
| 186 | + Assert.Equal("warmup", name); |
| 187 | + Assert.Equal(1, args.Count); |
| 188 | + Assert.IsType<WarmupContext>(args[triggerParamName]); |
| 189 | + |
| 190 | + functionInvoked = true; |
| 191 | + }) |
| 192 | + .Returns(Task.CompletedTask); |
| 193 | + scriptHostMock.SetupGet(p => p.Functions).Returns(functions); |
| 194 | + |
| 195 | + IActionResult response = await _hostController.Warmup(scriptHostMock.Object); |
| 196 | + |
| 197 | + Assert.Equal(warmupCalled, functionInvoked); |
| 198 | + Assert.IsType<OkResult>(response); |
| 199 | + } |
| 200 | + |
| 201 | + public class WarmupTestData : IEnumerable<object[]> |
| 202 | + { |
| 203 | + private readonly BindingMetadata _blobInputBinding; |
| 204 | + private readonly BindingMetadata _blobOutputBinding; |
| 205 | + private readonly BindingMetadata _blobTriggerBinding; |
| 206 | + private readonly BindingMetadata _warmupTriggerBinding; |
| 207 | + private readonly BindingMetadata _manualTriggerBinding; |
| 208 | + |
| 209 | + private readonly ParameterDescriptor _triggerParam; |
| 210 | + private readonly ParameterDescriptor _nonTriggerParam; |
| 211 | + |
| 212 | + private readonly FunctionDescriptor _warmupFunctionErrName; |
| 213 | + private readonly FunctionDescriptor _warmupFunctionWarmupName; |
| 214 | + private readonly FunctionDescriptor _manualFunctionWarmupName; |
| 215 | + private readonly FunctionDescriptor _blobFunctionBlobName; |
| 216 | + |
| 217 | + public WarmupTestData() |
| 218 | + { |
| 219 | + _triggerParam = new ParameterDescriptor("triggerParam", null) |
| 220 | + { |
| 221 | + IsTrigger = true |
| 222 | + }; |
| 223 | + |
| 224 | + _nonTriggerParam = new ParameterDescriptor("nonTriggerParam", null) |
| 225 | + { |
| 226 | + IsTrigger = false |
| 227 | + }; |
| 228 | + |
| 229 | + _blobInputBinding = GetBindingMetadata("boringBlob", "blob", BindingDirection.In); |
| 230 | + _blobOutputBinding = GetBindingMetadata("bigBlob", "blob", BindingDirection.Out); |
| 231 | + _blobTriggerBinding = GetBindingMetadata("beautifulBlob", "blobTrigger", BindingDirection.In); |
| 232 | + _warmupTriggerBinding = GetBindingMetadata("superState", "warmupTrigger", BindingDirection.In); |
| 233 | + _manualTriggerBinding = GetBindingMetadata("majesticManual", "manualTrigger", BindingDirection.In); |
| 234 | + |
| 235 | + var warmupMetadata = new Script.Description.FunctionMetadata(); |
| 236 | + warmupMetadata.Bindings.Add(_warmupTriggerBinding); |
| 237 | + warmupMetadata.Bindings.Add(_blobInputBinding); |
| 238 | + _warmupFunctionWarmupName = new FunctionDescriptor("warmup", null, warmupMetadata, |
| 239 | + new Collection<ParameterDescriptor>() { _nonTriggerParam, _triggerParam }, null, null, null); |
| 240 | + |
| 241 | + _warmupFunctionErrName = new FunctionDescriptor("donotwarmup", null, warmupMetadata, |
| 242 | + new Collection<ParameterDescriptor>() { _nonTriggerParam, _triggerParam }, null, null, null); |
| 243 | + |
| 244 | + var manualMetadata = new Script.Description.FunctionMetadata(); |
| 245 | + manualMetadata.Bindings.Add(_manualTriggerBinding); |
| 246 | + manualMetadata.Bindings.Add(_blobInputBinding); |
| 247 | + _manualFunctionWarmupName = new FunctionDescriptor("warmup", null, manualMetadata, |
| 248 | + new Collection<ParameterDescriptor>() { _nonTriggerParam, _triggerParam }, null, null, null); |
| 249 | + |
| 250 | + var blobMetadata = new Script.Description.FunctionMetadata(); |
| 251 | + blobMetadata.Bindings.Add(_blobTriggerBinding); |
| 252 | + blobMetadata.Bindings.Add(_blobOutputBinding); |
| 253 | + _blobFunctionBlobName = new FunctionDescriptor("blobFunction", null, blobMetadata, |
| 254 | + new Collection<ParameterDescriptor>() { _nonTriggerParam, _triggerParam }, null, null, null); |
| 255 | + } |
| 256 | + |
| 257 | + private BindingMetadata GetBindingMetadata(string name, string type, BindingDirection dir) |
| 258 | + { |
| 259 | + return new BindingMetadata() |
| 260 | + { |
| 261 | + Name = name, |
| 262 | + Type = type, |
| 263 | + Direction = dir |
| 264 | + }; |
| 265 | + } |
| 266 | + |
| 267 | + public IEnumerator<object[]> GetEnumerator() |
| 268 | + { |
| 269 | + return new List<object[]> |
| 270 | + { |
| 271 | + new object[] { new[] { _warmupFunctionWarmupName, _manualFunctionWarmupName }, true }, |
| 272 | + new object[] { new[] { _blobFunctionBlobName, _manualFunctionWarmupName }, false }, |
| 273 | + new object[] { new[] { _warmupFunctionErrName, _manualFunctionWarmupName }, false }, |
| 274 | + new object[] { new[] { _warmupFunctionWarmupName, _blobFunctionBlobName }, true } |
| 275 | + }.GetEnumerator(); |
| 276 | + } |
| 277 | + |
| 278 | + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); |
| 279 | + } |
169 | 280 | }
|
170 | 281 | }
|
0 commit comments