Skip to content

Commit

Permalink
Add test cases for writing concurrently in web session.
Browse files Browse the repository at this point in the history
  • Loading branch information
claudiamurialdo committed Dec 26, 2022
1 parent 02abda9 commit e1561b3
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,15 @@
<None Update="apps\configsettings.svc">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="apps\createsession.svc">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="apps\httpcors.svc">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="apps\returnsession.svc">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="apps\saveimage.svc">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public MiddlewareTest()
{
GXRouting.ContentRootPath = Directory.GetCurrentDirectory();
server = new TestServer(WebHost.CreateDefaultBuilder().UseStartup<Startup>().UseEnvironment(DOTNET_ENVIRONMENT));
server.PreserveExecutionContext= true;
server.CreateClient();
}
protected string GetHeader(HttpResponseMessage response, string headerName)
Expand Down
85 changes: 85 additions & 0 deletions dotnet/test/DotNetCoreWebUnitTest/Middleware/SessionTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.Net.Http.Headers;
using Xunit;
namespace xUnitTesting
{
public class SessionTest : MiddlewareTest
{
public SessionTest() : base()
{
ClassLoader.FindType("apps.createsession", "GeneXus.Programs.apps", "createsession", Assembly.GetExecutingAssembly(), true);//Force loading assembly for append createsession
ClassLoader.FindType("apps.returnsession", "GeneXus.Programs.apps", "returnsession", Assembly.GetExecutingAssembly(), true);//Force loading assembly for saveimage returnsession
server.AllowSynchronousIO = true;
}
//[Fact]
public void TestConcurrentRequest()
{
HttpClient client = server.CreateClient();
CreateSession(client).Wait();
TestSessionGrids(client).Wait();

}
CookieHeaderValue SessionCookie;
async Task CreateSession(HttpClient client)
{
string[] url1 = new string[]{ "rest/apps/createsession?gxid=48796585&Type=C1&Title=Component",
"rest/apps/createsession?gxid=20200694&Type=C3&Title=Component%203",
"rest/apps/createsession?gxid=30382714&Type=C2&Title=Component%202",
"rest/apps/createsession?gxid=9559029&Type=C4&Title=Component%204",
"rest/apps/createsession?gxid=51975664&Type=C5&Title=Component%205"};

//Fix sessionID
var c = await client.GetAsync(url1[0]);
string result = await c.Content.ReadAsStringAsync();
Console.WriteLine(result);
foreach (var header in c.Headers)
{
if (header.Key == "Set-Cookie")
{
string cookieValue = header.Value.First();
string[] cookieValues = cookieValue.Split(';', '=');
SessionCookie = new CookieHeaderValue(cookieValues[0], cookieValues[1]);
client.DefaultRequestHeaders.Add("Cookie", SessionCookie.ToString());
}
}
List<Task> tasks = new List<Task>();
foreach (string s1 in url1)
{
tasks.Add(Task.Run(() => ExecuteGet(client, s1)));
}

await Task.WhenAll(tasks);
}
async Task ExecuteGet(HttpClient client, string url)
{
var r = await client.GetAsync(url);
}
async Task ExecuteGetGrid(HttpClient client, string url)
{
var r = await client.GetAsync(url);
string result = await r.Content.ReadAsStringAsync();
Assert.Contains(" - type ", result, StringComparison.OrdinalIgnoreCase);
Console.WriteLine(url + ":" + result);
}
async Task TestSessionGrids(HttpClient client)
{
string[] url2 = new string[] {"rest/apps/returnsession?gxid=48796585&Type=C1&Title=Component%201&start=0&count=30",
"rest/apps/returnsession?gxid=30382714&Type=C2&Title=Component%202&start=0&count=30",
"rest/apps/returnsession?gxid=51975664&Type=C5&Title=Component%205&start=0&count=30",
"rest/apps/returnsession?gxid=9559029&Type=C4&Title=Component%204&start=0&count=30",
"rest/apps/returnsession?gxid=20200694&Type=C3&Title=Component%203&start=0&count=30" };
List<Task> tasksGrid = new List<Task>();
foreach (string s1 in url2)
{
tasksGrid.Add(Task.Run(() => ExecuteGetGrid(client, s1)));
}
await Task.WhenAll(tasksGrid);
}
}

}
69 changes: 69 additions & 0 deletions dotnet/test/DotNetCoreWebUnitTest/apps/createsession.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using GeneXus.Application;
using GeneXus.Data.NTier;
using GeneXus.Http;
using GeneXus.Http.Server;
using GeneXus.Procedure;

namespace GeneXus.Programs.apps
{
public class createsession : GXWebProcedure
{

public createsession()
{
context = new GxContext();
DataStoreUtil.LoadDataStores(context);
IsMain = true;
context.SetDefaultTheme("Carmine");
}

public createsession(IGxContext context)
{
this.context = context;
IsMain = false;
}

public void execute(string gxid, string Type, string title)
{
initialize();
executePrivate(gxid, Type, title);
}

void executePrivate(string gxid, string Type, string Title)
{
string Gxids = "gxid_" + gxid;
Gxwebsession.Set("GXID_" + gxid + "GXVAR_DATASDT", gxid);
if (string.IsNullOrEmpty(Gxwebsession.Get(Gxids)))
{

Gxwebsession.Set(Gxids + "gxvar_Datasdt", $"Reccord 1 - type {Type}");
Gxwebsession.Set(Gxids + "gxvar_Data1", "");
Gxwebsession.Set(Gxids + "gxvar_Data2", "");
Gxwebsession.Set(Gxids, "true");
}
this.cleanup();
}

public override void cleanup()
{
CloseOpenCursors();
base.cleanup();
if (IsMain)
{
context.CloseConnections();
}
ExitApp();
}

protected void CloseOpenCursors()
{
}

public override void initialize()
{
Gxwebsession = context.GetSession();
}
private IGxSession Gxwebsession;
}

}
1 change: 1 addition & 0 deletions dotnet/test/DotNetCoreWebUnitTest/apps/createsession.svc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%@ServiceHost Service= "GeneXus.Programs.apps.createsession,apps.createsession" %>
63 changes: 63 additions & 0 deletions dotnet/test/DotNetCoreWebUnitTest/apps/returnsession.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using GeneXus.Application;
using GeneXus.Data.NTier;
using GeneXus.Http;
using GeneXus.Http.Server;
using GeneXus.Procedure;
using Newtonsoft.Json.Linq;

namespace GeneXus.Programs.apps
{
public class returnsession : GXWebProcedure
{

public returnsession()
{
context = new GxContext();
DataStoreUtil.LoadDataStores(context);
IsMain = true;
context.SetDefaultTheme("Carmine");
}

public returnsession(IGxContext context)
{
this.context = context;
IsMain = false;
}

public void execute(string gxid, out string result)
{
initialize();
executePrivate(gxid, out result);
}

void executePrivate(string gxid, out string result)
{
string Gxids = "gxid_" + gxid;
string actual = Gxwebsession.Get(Gxids + "gxvar_Datasdt");
result = $"{actual},{gxid}";
this.cleanup();
}

public override void cleanup()
{
CloseOpenCursors();
base.cleanup();
if (IsMain)
{
context.CloseConnections();
}
ExitApp();
}

protected void CloseOpenCursors()
{
}

public override void initialize()
{
Gxwebsession = context.GetSession();
}
private IGxSession Gxwebsession;
}

}
1 change: 1 addition & 0 deletions dotnet/test/DotNetCoreWebUnitTest/apps/returnsession.svc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%@ServiceHost Service= "GeneXus.Programs.apps.returnsession,apps.returnsession" %>

0 comments on commit e1561b3

Please sign in to comment.