Esempio n. 1
0
        public void UserAccountCacheTest( )
        {
            const string username = "******";
            const string password = "******";

            UserAccount account = null;

            try
            {
                UserAccount existingAccount = Entity.GetByName <UserAccount>(username, true).FirstOrDefault( );

                if (existingAccount != null)
                {
                    existingAccount.Delete( );
                }

                account = new UserAccount
                {
                    Name               = username,
                    Password           = password,
                    AccountStatus_Enum = UserAccountStatusEnum_Enumeration.Active
                };

                account.Save( );

                Assert.AreEqual(0, GetUserAccountCacheCount( ));
                Assert.AreEqual(0, GetUserAccountCacheTenantCount(RunAsDefaultTenant.DefaultTenantName));

                UserAccountCache.GetRequestContext(username, password, RunAsDefaultTenant.DefaultTenantName);

                Assert.AreEqual(1, GetUserAccountCacheCount( ));
                Assert.AreEqual(1, GetUserAccountCacheTenantCount(RunAsDefaultTenant.DefaultTenantName));

                using (var domain = new TestAppDomain( ))
                {
                    Func <MessageEventArgs <TestMessage>, bool> predicate = m => m.Message.Action == "EDC" && m.Message.Items.Contains(username);

                    var instance = domain.InjectType <SubscriberRefObject <TestMessage> >(UserAccountCache.CacheName, RunAsDefaultTenant.DefaultTenantName, predicate);

                    UserAccountCache.Invalidate(username, RunAsDefaultTenant.DefaultTenantName);

                    bool waitOne = instance.MessageReceived.WaitOne(DefaultTimeout);

                    Assert.IsTrue(waitOne, "No message received in " + DefaultTimeout + "ms.");

                    Assert.GreaterOrEqual(instance.ReceivedMessages.Count, 1);

                    MessageEventArgs <TestMessage> message = instance.ReceivedMessages.FirstOrDefault(predicate);

                    Assert.IsNotNull(message, "Received message is invalid");
                }
            }
            finally
            {
                if (account != null)
                {
                    account.Delete( );
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        ///     Invalidates the specified tenant.
        /// </summary>
        /// <param name="tenant">The tenant.</param>
        public static void Invalidate(IEntityRef tenant)
        {
            if (tenant == null)
            {
                return;
            }

            using (new SecurityBypassContext())
            {
                using (new EntityTypeContext( ))
                {
                    /////
                    // Invalidate all the caches that could possible hold this entity.
                    /////
                    var ids = GetEntityIdentifiers(tenant.Id);

                    using (new DeferredChannelMessageContext( ))
                    {
                        foreach (var id in ids)
                        {
                            /////
                            // EntityCache implicitly removes from the EntityFieldCache
                            // as well as the EntityRelationshipCache (in both directions).
                            /////
                            EntityCache.Instance.Remove(id);
                        }
                    }


                    InvalidateLocalProcessImpl(tenant.Id);

                    using (var channel = CreateMessageChannel( ))
                    {
                        if (channel != null)
                        {
                            var message = new TenantHelperMessage
                            {
                                MessageType = TenantHelperMessageType.InvalidateTenant,
                                TenantId    = tenant.Id
                            };

                            message.EntityIds.UnionWith(ids);

                            channel.Publish(message, PublishOptions.None, false, MergeMessages);
                        }
                    }
                }

                string tenantName;

                using (new TenantAdministratorContext(0))
                {
                    tenantName = Entity.GetName(tenant.Id);
                }

                UserAccountCache.Invalidate(tenantName);
            }
        }