Skip to content

Commit

Permalink
Merge branch 'release/0.107.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Jericho committed Apr 18, 2024
2 parents 4019ab6 + 57cc79e commit 87a1e84
Show file tree
Hide file tree
Showing 35 changed files with 1,012 additions and 486 deletions.
44 changes: 0 additions & 44 deletions Source/StrongGrid.Benchmark/Program.cs
Original file line number Diff line number Diff line change
@@ -1,62 +1,18 @@
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Running;
using System.Linq;

namespace StrongGrid.Benchmark
{
class Program
{
static void Main(string[] args)
{
//var serializerContext = GenerateAttributesForSerializerContext();

IConfig config = null;

// To debug
//config = new DebugInProcessConfig();

BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args, config);
}

private static string GenerateAttributesForSerializerContext()
{
// Handy code to generate the 'JsonSerializable' attributes for StrongGridJsonSerializerContext
var baseNamespace = "StrongGrid.Models";
var allTypes = System.Reflection.Assembly
.GetAssembly(typeof(Client))
.GetTypes()
.Where(t => t.IsClass || t.IsEnum)
.Where(t => !string.IsNullOrEmpty(t.Namespace))
.Where(t => t.Namespace.StartsWith(baseNamespace));

var typesInBaseNamespace = allTypes
.Where(t => t.Namespace.Equals(baseNamespace))
.Select(t => new
{
Type = t,
JsonSerializeAttribute = $"[JsonSerializable(typeof({t.FullName}))]",
JsonSerializeAttributeArray = $"[JsonSerializable(typeof({t.FullName}[]))]",
JsonSerializeAttributeNullable = t.IsEnum ? $"[JsonSerializable(typeof({t.FullName}?))]" : string.Empty,
});

var typesInSubNamespace = allTypes
.Where(t => !t.Namespace.Equals(baseNamespace))
.Select(t => new
{
Type = t,
JsonSerializeAttribute = $"[JsonSerializable(typeof({t.FullName}), TypeInfoPropertyName = \"{t.FullName.Remove(0, baseNamespace.Length + 1).Replace(".", "")}\")]",
JsonSerializeAttributeArray = $"[JsonSerializable(typeof({t.FullName}[]), TypeInfoPropertyName = \"{t.FullName.Remove(0, baseNamespace.Length + 1).Replace(".", "")}Array\")]",
JsonSerializeAttributeNullable = t.IsEnum ? $"[JsonSerializable(typeof({t.FullName}?), TypeInfoPropertyName = \"{t.FullName.Remove(0, baseNamespace.Length + 1).Replace(".", "")}Nullable\")]" : string.Empty,
});

var typesSortedAlphabetically = typesInBaseNamespace.Union(typesInSubNamespace).OrderBy(t => t.Type.FullName);

var simpleAttributes = string.Join("\r\n", typesSortedAlphabetically.Where(t => !string.IsNullOrEmpty(t.JsonSerializeAttribute)).Select(t => t.JsonSerializeAttribute));
var arrayAttributes = string.Join("\r\n", typesSortedAlphabetically.Where(t => !string.IsNullOrEmpty(t.JsonSerializeAttributeArray)).Select(t => t.JsonSerializeAttributeArray));
var nullableAttributes = string.Join("\r\n", typesSortedAlphabetically.Where(t => !string.IsNullOrEmpty(t.JsonSerializeAttributeNullable)).Select(t => t.JsonSerializeAttributeNullable));

var result = string.Join("\r\n\r\n", new[] { simpleAttributes, arrayAttributes, nullableAttributes });
return result;
}
}
}
52 changes: 0 additions & 52 deletions Source/StrongGrid.IntegrationTests/NativeMethods.cs

This file was deleted.

164 changes: 149 additions & 15 deletions Source/StrongGrid.IntegrationTests/Program.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,89 @@
using Logzio.DotNet.NLog;
using Microsoft.ApplicationInsights.NLogTarget;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using NLog;
using NLog.Config;
using NLog.Extensions.Logging;
using NLog.Layouts;
using NLog.Targets;
using System;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace StrongGrid.IntegrationTests
{
public class Program
{
public static async Task<int> Main()
public static async Task Main(string[] args)
{
var services = new ServiceCollection();
ConfigureServices(services);
using var serviceProvider = services.BuildServiceProvider();
var app = serviceProvider.GetService<TestsRunner>();
return await app.RunAsync().ConfigureAwait(false);
var serializerContext = GenerateAttributesForSerializerContext();

var builder = Host.CreateApplicationBuilder();
builder.Services.AddHostedService<TestsRunner>();

// Configure cancellation (this allows you to press CTRL+C or CTRL+Break to stop the integration tests)
var cts = new CancellationTokenSource();
Console.CancelKeyPress += (s, e) =>
{
e.Cancel = true;
cts.Cancel();
};

// Configure logging
builder.Logging.ClearProviders(); // Remove the built-in providers (which include the Console)
builder.Logging.AddNLog(GetNLogConfiguration()); // Add our desired custom providers (which include the Colored Console)

// Run the tests
var host = builder.Build();
await host.StartAsync(cts.Token).ConfigureAwait(false);

// Stop NLog (which has the desirable side-effect of flushing any pending logs)
LogManager.Shutdown();
}

private static void ConfigureServices(ServiceCollection services)
private static string GenerateAttributesForSerializerContext()
{
services
.AddLogging(loggingBuilder => loggingBuilder.AddNLog(GetNLogConfiguration()))
.AddTransient<TestsRunner>();
// Handy code to generate the 'JsonSerializable' attributes for ZoomNetJsonSerializerContext
var baseNamespace = "StrongGrid.Models";
var allTypes = System.Reflection.Assembly
.GetAssembly(typeof(Client))
.GetTypes()
.Where(t => t.IsClass || t.IsEnum)
.Where(t => !string.IsNullOrEmpty(t.Namespace))
.Where(t => t.Namespace.StartsWith(baseNamespace));

var typesInBaseNamespace = allTypes
.Where(t => t.Namespace.Equals(baseNamespace))
.Select(t => new
{
Type = t,
JsonSerializeAttribute = $"[JsonSerializable(typeof({t.FullName}))]",
JsonSerializeAttributeArray = $"[JsonSerializable(typeof({t.FullName}[]))]",
JsonSerializeAttributeNullable = t.IsEnum ? $"[JsonSerializable(typeof({t.FullName}?))]" : string.Empty,
});

var typesInSubNamespace = allTypes
.Where(t => !t.Namespace.Equals(baseNamespace))
.Select(t => new
{
Type = t,
JsonSerializeAttribute = $"[JsonSerializable(typeof({t.FullName}), TypeInfoPropertyName = \"{t.FullName.Remove(0, baseNamespace.Length + 1).Replace(".", "")}\")]",
JsonSerializeAttributeArray = $"[JsonSerializable(typeof({t.FullName}[]), TypeInfoPropertyName = \"{t.FullName.Remove(0, baseNamespace.Length + 1).Replace(".", "")}Array\")]",
JsonSerializeAttributeNullable = t.IsEnum ? $"[JsonSerializable(typeof({t.FullName}?), TypeInfoPropertyName = \"{t.FullName.Remove(0, baseNamespace.Length + 1).Replace(".", "")}Nullable\")]" : string.Empty,
});

var typesSortedAlphabetically = typesInBaseNamespace.Union(typesInSubNamespace).OrderBy(t => t.Type.FullName);

var simpleAttributes = string.Join("\r\n", typesSortedAlphabetically.Where(t => !string.IsNullOrEmpty(t.JsonSerializeAttribute)).Select(t => t.JsonSerializeAttribute));
var arrayAttributes = string.Join("\r\n", typesSortedAlphabetically.Where(t => !string.IsNullOrEmpty(t.JsonSerializeAttributeArray)).Select(t => t.JsonSerializeAttributeArray));
var nullableAttributes = string.Join("\r\n", typesSortedAlphabetically.Where(t => !string.IsNullOrEmpty(t.JsonSerializeAttributeNullable)).Select(t => t.JsonSerializeAttributeNullable));

var result = string.Join("\r\n\r\n", [simpleAttributes, arrayAttributes, nullableAttributes]);
return result;
}

private static LoggingConfiguration GetNLogConfiguration()
Expand All @@ -35,18 +95,92 @@ private static LoggingConfiguration GetNLogConfiguration()
var logzioToken = Environment.GetEnvironmentVariable("LOGZIO_TOKEN");
if (!string.IsNullOrEmpty(logzioToken))
{
var logzioTarget = new LogzioTarget { Token = logzioToken };
logzioTarget.ContextProperties.Add(new TargetPropertyWithContext("source", "StrongGrid_integration_tests"));
logzioTarget.ContextProperties.Add(new TargetPropertyWithContext("StrongGrid-Version", StrongGrid.Client.Version));
var logzioTarget = new LogzioTarget
{
Name = "Logzio",
Token = logzioToken,
LogzioType = "nlog",
JsonKeysCamelCase = true,
// ProxyAddress = "http://localhost:8888",
};
logzioTarget.ContextProperties.Add(new NLog.Targets.TargetPropertyWithContext("Source", "StrongGrid_integration_tests"));
logzioTarget.ContextProperties.Add(new NLog.Targets.TargetPropertyWithContext("StrongGrid-Version", StrongGrid.Client.Version));

nLogConfig.AddTarget("Logzio", logzioTarget);
nLogConfig.AddRule(NLog.LogLevel.Debug, NLog.LogLevel.Fatal, "Logzio", "*");
nLogConfig.AddRule(NLog.LogLevel.Info, NLog.LogLevel.Fatal, logzioTarget, "*");
}

// Send logs to Azure Insights
var instrumentationKey = Environment.GetEnvironmentVariable("APPLICATIONINSIGHTS_INSTRUMENTATION_KEY");
if (!string.IsNullOrEmpty(instrumentationKey))
{
var applicationInsightsTarget = new ApplicationInsightsTarget() { InstrumentationKey = instrumentationKey, Name = "StrongGrid" };
applicationInsightsTarget.ContextProperties.Add(new Microsoft.ApplicationInsights.NLogTarget.TargetPropertyWithContext("Source", "StrongGrid_integration_tests"));
applicationInsightsTarget.ContextProperties.Add(new Microsoft.ApplicationInsights.NLogTarget.TargetPropertyWithContext("StrongGrid-Version", StrongGrid.Client.Version));

nLogConfig.AddTarget("ApplicationInsights", applicationInsightsTarget);
nLogConfig.AddRule(NLog.LogLevel.Info, NLog.LogLevel.Fatal, applicationInsightsTarget, "*");
}

// Send logs to DataDog
var datadogKey = Environment.GetEnvironmentVariable("DATADOG_APIKEY");
if (!string.IsNullOrEmpty(datadogKey))
{
var datadogTarget = new WebServiceTarget("datadog")
{
Url = "https://http-intake.logs.us5.datadoghq.com/v1/input",
Encoding = Encoding.UTF8,
Protocol = WebServiceProtocol.JsonPost,
PreAuthenticate = false
};

// DD_API_KEY
// Your Datadog API Key for sending your logs to Datadog.
datadogTarget.Headers.Add(new MethodCallParameter("DD-API-KEY", Layout.FromString(datadogKey)));

// DD_SITE
// The name of your Datadog site.Choose from one of the following examples:
// Example: datadoghq.com(US1), datadoghq.eu(EU), us3.datadoghq.com(US3), us5.datadoghq.com(US5), ddog - gov.com(US1 - FED)
// Default: datadoghq.com(US1)
datadogTarget.Headers.Add(new MethodCallParameter("DD_SITE", Layout.FromString("us5.datadoghq.com(US5)")));

// DD_LOGS_DIRECT_SUBMISSION_INTEGRATIONS
// Enables Agentless logging.Enable for your logging framework by setting to Serilog, NLog, Log4Net, or ILogger(for Microsoft.Extensions.Logging).
// If you are using multiple logging frameworks, use a semicolon separated list of variables.
// Example: Serilog; Log4Net; NLog
datadogTarget.Headers.Add(new MethodCallParameter("DD_LOGS_DIRECT_SUBMISSION_INTEGRATIONS", Layout.FromString("NLog")));

// DD_LOGS_DIRECT_SUBMISSION_SOURCE
// Sets the parsing rule for submitted logs.
// Should always be set to csharp, unless you have a custom pipeline.
// Default: csharp
datadogTarget.Headers.Add(new MethodCallParameter("DD_LOGS_DIRECT_SUBMISSION_SOURCE", Layout.FromString("csharp")));

// DD_LOGS_DIRECT_SUBMISSION_MAX_BATCH_SIZE
// Sets the maximum number of logs to send at one time.
// Takes into account the limits in place for the API.
// Default: 1000

// DD_LOGS_DIRECT_SUBMISSION_MAX_QUEUE_SIZE
// Sets the maximum number of logs to hold in the internal queue at any one time before dropping log messages.
// Default: 100000

// DD_LOGS_DIRECT_SUBMISSION_BATCH_PERIOD_SECONDS
// Sets the time to wait(in seconds) before checking for new logs to send.
// Default: 1

datadogTarget.Headers.Add(new MethodCallParameter("Content-Type", Layout.FromString("application/json")));
datadogTarget.Headers.Add(new MethodCallParameter("Source", "StrongGrid_integration_tests"));
datadogTarget.Headers.Add(new MethodCallParameter("StrongGrid-Version", StrongGrid.Client.Version));

nLogConfig.AddTarget("DataDog", datadogTarget);
nLogConfig.AddRule(NLog.LogLevel.Info, NLog.LogLevel.Fatal, datadogTarget, "*");
}

// Send logs to console
var consoleTarget = new ColoredConsoleTarget();
nLogConfig.AddTarget("ColoredConsole", consoleTarget);
nLogConfig.AddRule(NLog.LogLevel.Warn, NLog.LogLevel.Fatal, "ColoredConsole", "*");
nLogConfig.AddRule(NLog.LogLevel.Info, NLog.LogLevel.Fatal, consoleTarget, "*");

return nLogConfig;
}
Expand Down
9 changes: 9 additions & 0 deletions Source/StrongGrid.IntegrationTests/ResultCodes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace StrongGrid.IntegrationTests
{
internal enum ResultCodes
{
Success = 0,
Exception = 1,
Cancelled = 1223
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<AssemblyName>StrongGrid.IntegrationTests</AssemblyName>
<RootNamespace>StrongGrid.IntegrationTests</RootNamespace>
</PropertyGroup>
Expand All @@ -13,9 +13,15 @@

<ItemGroup>
<PackageReference Include="Logzio.DotNet.NLog" Version="1.1.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.ApplicationInsights.NLogTarget" Version="2.22.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.3.8" />

<!-- This is a workaround for the problem described here: https://github.com/logzio/logzio-dotnet/issues/72 -->
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions Source/StrongGrid.IntegrationTests/Tests/AccessManagement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public async Task RunAsync(IBaseClient client, TextWriter log, CancellationToken
}

// ========== VERY IMPORTANT ==========
// You must manually whitelist your IP address in your SendGrid account in the web interface before we
// attempt to whitelist an IP via the API. Otherwise, whitelisting an IP address could effectively lock
// You must manually whitelist at least one IP address in your SendGrid account in the web interface before
// we attempt to whitelist your IP via the API. Otherwise, whitelisting an IP address could effectively lock
// you out of your own account. Trust me, it happened to me and it took a week of back and forth with
// SendGrid support before they agreed that I was the legitimate owner of my own account and they restored
// access to my account. That's the reason why the following code will only run if we find other whitelisted
Expand All @@ -47,7 +47,7 @@ public async Task RunAsync(IBaseClient client, TextWriter log, CancellationToken
await log.WriteLineAsync("").ConfigureAwait(false);
await log.WriteLineAsync("CAUTION: do not attempt to manually configure whitelisted IP addresses if you are unsure how to do it or if you").ConfigureAwait(false);
await log.WriteLineAsync("don't know how to get your public IP address or if you suspect your ISP may change your assigned IP address from").ConfigureAwait(false);
await log.WriteLineAsync("time to time because there is a strong posibility you could lock yourself out your account.").ConfigureAwait(false);
await log.WriteLineAsync("time to time because THERE IS A STRONG POSSIBILITY YOU COULD LOCK YOURSELF OUT OF YOUR ACCOUNT.").ConfigureAwait(false);
await log.WriteLineAsync("========================================================================\n").ConfigureAwait(false);
}
else
Expand Down
6 changes: 3 additions & 3 deletions Source/StrongGrid.IntegrationTests/Tests/Designs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ public async Task RunAsync(IBaseClient client, TextWriter log, CancellationToken
await Task.WhenAll(cleanUpTasks).ConfigureAwait(false);

// GET PRE_BUILT DESIGNS
var prebuiltDesigns = await client.Designs.GetAllPrebuiltAsync(2, null, cancellationToken).ConfigureAwait(false);
var prebuiltDesigns = await client.Designs.GetAllPrebuiltAsync(25, null, cancellationToken).ConfigureAwait(false);
await log.WriteLineAsync($"{prebuiltDesigns.Records.Length} pre-built designs retrieved. There are a total of {prebuiltDesigns.TotalRecords} designs.").ConfigureAwait(false);

// TEST PAGINATION
if (!string.IsNullOrEmpty(prebuiltDesigns.NextPageToken))
while (!string.IsNullOrEmpty(prebuiltDesigns.NextPageToken))
{
prebuiltDesigns = await client.Designs.GetAllPrebuiltAsync(2, prebuiltDesigns.NextPageToken, cancellationToken).ConfigureAwait(false);
prebuiltDesigns = await client.Designs.GetAllPrebuiltAsync(25, prebuiltDesigns.NextPageToken, cancellationToken).ConfigureAwait(false);
await log.WriteLineAsync($"Retrieved {prebuiltDesigns.Records.Length} more pre-built designs.").ConfigureAwait(false);
}

Expand Down
Loading

0 comments on commit 87a1e84

Please sign in to comment.