예제 #1
0
        public async Task WillGetSessionIdFromSessionLogsAsync()
        {
            GlobalContext.Properties["sessionId"] = new SessionIdCapturer();

            using (var spy = new TextLogSpy("NHibernate.SQL", "%message | SessionId: %property{sessionId}"))
                using (var s = Sfi.OpenSession())
                {
                    var sessionId = ((SessionImpl)s).SessionId;

                    await(s.GetAsync <Person>(1));             //will execute some sql

                    var loggingEvent = spy.GetWholeLog();
                    Assert.That(loggingEvent.Contains(sessionId.ToString()), Is.True);
                }
        }
예제 #2
0
        public async Task WillGetSessionIdFromInterlacedSessionsLogsAsync()
        {
            GlobalContext.Properties["sessionId"] = new SessionIdCapturer();
            var interceptor = new InterlacedSessionInterceptor(Sfi);

            using (var spy = new TextLogSpy("NHibernate.SQL", "%message | SessionId: %property{sessionId}"))
                using (var s = Sfi.WithOptions().Interceptor(interceptor).OpenSession())
                {
                    // Trigger an operation which will fire many interceptor events, before and after s own logging.
                    var persons = await(s.Query <Person>().ToListAsync());

                    var loggingEvent = spy.GetWholeLog();
                    for (var i = 0; i < interceptor.SessionIds.Count; i++)
                    {
                        var sessionId = interceptor.SessionIds[i];
                        Assert.That(loggingEvent, Does.Contain($"p0 = {i + 1} [Type: Int32 (0:0:0)] | SessionId: {sessionId}"));
                    }
                    Assert.That(loggingEvent, Does.Contain($"Person person0_ | SessionId: {s.GetSessionImplementation().SessionId}"));
                }
        }
예제 #3
0
        public async Task WillGetSessionIdFromConsecutiveSessionsLogsAsync()
        {
            GlobalContext.Properties["sessionId"] = new SessionIdCapturer();

            using (var spy = new TextLogSpy("NHibernate.SQL", "%message | SessionId: %property{sessionId}"))
            {
                var sessions = Enumerable.Range(1, 10).Select(i => Sfi.OpenSession()).ToArray();
                try
                {
                    for (var i = 0; i < 10; i++)
                    {
                        for (var j = 0; j < 10; j++)
                        {
                            var s = sessions[j];
                            await(s.GetAsync <Person>(i * 10 + j));                     //will execute some sql
                        }
                    }
                }
                finally
                {
                    foreach (var s in sessions)
                    {
                        s.Dispose();
                    }
                }

                var loggingEvent = spy.GetWholeLog();
                for (var i = 0; i < 10; i++)
                {
                    for (var j = 0; j < 10; j++)
                    {
                        var sessionId = sessions[j].GetSessionImplementation().SessionId;
                        Assert.That(loggingEvent, Does.Contain($"p0 = {i * 10 + j} [Type: Int32 (0:0:0)] | SessionId: {sessionId}"));
                    }
                }
            }
        }
예제 #4
0
        public async Task WillGetSessionIdFromSessionLogsConcurrentAsync()
        {
            if (!TestDialect.SupportsConcurrencyTests)
            {
                Assert.Ignore($"Dialect {Dialect} does not supports concurrency tests");
            }

            GlobalContext.Properties["sessionId"] = new SessionIdCapturer();

            // Do not use a ManualResetEventSlim, it does not support async and exhausts the task thread pool in the
            // async counterparts of this test. SemaphoreSlim has the async support and release the thread when waiting.
            var semaphore  = new SemaphoreSlim(0);
            var failures   = new ConcurrentBag <Exception>();
            var sessionIds = new ConcurrentDictionary <int, Guid>();

            using (var spy = new TextLogSpy("NHibernate.SQL", "%message | SessionId: %property{sessionId}"))
            {
                await(Task.WhenAll(
                          Enumerable.Range(1, 12 - 1).Select(async i =>
                {
                    if (i > 10)
                    {
                        // Give some time to threads for reaching the wait, having all of them ready to do most of their job concurrently.
                        await(Task.Delay(100));
                        semaphore.Release(10);
                        return;
                    }
                    try
                    {
                        using (var s = Sfi.OpenSession())
                        {
                            sessionIds.AddOrUpdate(
                                i,
                                s.GetSessionImplementation().SessionId,
                                (ti, old) => throw new InvalidOperationException(
                                    $"Thread number {ti} has already session id {old}, while attempting to set it to" +
                                    $" {s.GetSessionImplementation().SessionId}"));
                            await(semaphore.WaitAsync());

                            for (int j = 0; j < 10; j++)
                            {
                                await(s.GetAsync <Person>(i * 10 + j));                                         //will execute some sql
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        failures.Add(e);
                    }
                })));

                Assert.That(failures, Is.Empty, $"{failures.Count} task(s) failed.");

                var loggingEvent = spy.GetWholeLog();
                for (var i = 1; i < 11; i++)
                {
                    for (var j = 0; j < 10; j++)
                    {
                        var sessionId = sessionIds[i];
                        Assert.That(loggingEvent, Does.Contain($"p0 = {i * 10 + j} [Type: Int32 (0:0:0)] | SessionId: {sessionId}"));
                    }
                }
            }
        }