From 226c082ed417f4a5f2295595e45eca2fcb1e42c9 Mon Sep 17 00:00:00 2001 From: Justin Clark-Casey (justincc) Date: Tue, 11 Aug 2009 16:45:16 +0100 Subject: Establish CachedUserInfo.OnInventoryReceived event so that region/test inventory code can be written with the async inventory fetch --- .../Communications/Cache/CachedUserInfo.cs | 19 ++++- .../Tests/Cache/UserProfileCacheServiceTests.cs | 96 ++++++++++++---------- 2 files changed, 68 insertions(+), 47 deletions(-) (limited to 'OpenSim/Framework/Communications') diff --git a/OpenSim/Framework/Communications/Cache/CachedUserInfo.cs b/OpenSim/Framework/Communications/Cache/CachedUserInfo.cs index 10aff42..8ee1b1a 100644 --- a/OpenSim/Framework/Communications/Cache/CachedUserInfo.cs +++ b/OpenSim/Framework/Communications/Cache/CachedUserInfo.cs @@ -49,16 +49,24 @@ namespace OpenSim.Framework.Communications.Cache IClientAPI client, UUID folderID, bool fetchFolders, bool fetchItems); public delegate void OnItemReceivedDelegate(UUID itemID); + public delegate void OnInventoryReceivedDelegate(UUID userID); /// /// Stores user profile and inventory data received from backend services for a particular user. /// public class CachedUserInfo - { + { + private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); + + //// + /// Fired when a particular item has been received from the inventory service + /// public event OnItemReceivedDelegate OnItemReceived; - private static readonly ILog m_log - = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); + /// + /// Fired once the entire inventory has been received for the user + /// + public event OnInventoryReceivedDelegate OnInventoryReceived; /// /// The comms manager holds references to services (user, grid, inventory, etc.) @@ -133,7 +141,9 @@ namespace OpenSim.Framework.Communications.Cache UUID parentFolderId = folder.ParentID; if (dictionary.ContainsKey(parentFolderId)) + { dictionary[parentFolderId].Add(folder); + } else { IList folders = new List(); @@ -299,6 +309,9 @@ namespace OpenSim.Framework.Communications.Cache request.Execute(); } } + + if (OnInventoryReceived != null) + OnInventoryReceived(UserProfile.ID); } /// diff --git a/OpenSim/Framework/Communications/Tests/Cache/UserProfileCacheServiceTests.cs b/OpenSim/Framework/Communications/Tests/Cache/UserProfileCacheServiceTests.cs index 0402883..fe88cf5 100644 --- a/OpenSim/Framework/Communications/Tests/Cache/UserProfileCacheServiceTests.cs +++ b/OpenSim/Framework/Communications/Tests/Cache/UserProfileCacheServiceTests.cs @@ -43,6 +43,18 @@ namespace OpenSim.Framework.Communications.Tests [TestFixture] public class UserProfileCacheServiceTests { + /// Used by tests to indicate whether an async operation timed out + private bool timedOut; + + private void InventoryReceived(UUID userId) + { + lock (this) + { + timedOut = false; + Monitor.PulseAll(this); + } + } + [Test] public void TestGetUserDetails() { @@ -118,14 +130,15 @@ namespace OpenSim.Framework.Communications.Tests TestHelper.InMethod(); Scene myScene = SceneSetupHelpers.SetupScene("inventory"); - CachedUserInfo userInfo = UserProfileTestUtils.CreateUserWithInventory(myScene.CommsManager); - for (int i = 0 ; i < 50 ; i++) - { - if (userInfo.HasReceivedInventory == true) - break; - Thread.Sleep(200); - } - Assert.That(userInfo.HasReceivedInventory, Is.True, "FetchInventory timed out (10 seconds)"); + + timedOut = true; + lock (this) + { + UserProfileTestUtils.CreateUserWithInventory(myScene.CommsManager, InventoryReceived); + Monitor.Wait(this, 60000); + } + + Assert.That(timedOut, Is.False, "Timed out"); } [Test] @@ -134,14 +147,13 @@ namespace OpenSim.Framework.Communications.Tests TestHelper.InMethod(); Scene myScene = SceneSetupHelpers.SetupScene("inventory"); - CachedUserInfo userInfo = UserProfileTestUtils.CreateUserWithInventory(myScene.CommsManager); - for (int i = 0 ; i < 50 ; i++) - { - if (userInfo.HasReceivedInventory == true) - break; - Thread.Sleep(200); + CachedUserInfo userInfo; + + lock (this) + { + userInfo = UserProfileTestUtils.CreateUserWithInventory(myScene.CommsManager, InventoryReceived); + Monitor.Wait(this, 60000); } - Assert.That(userInfo.HasReceivedInventory, Is.True, "FetchInventory timed out (10 seconds)"); UUID folderId = UUID.Parse("00000000-0000-0000-0000-000000000011"); Assert.That(userInfo.RootFolder.GetChildFolder(folderId), Is.Null); @@ -156,14 +168,13 @@ namespace OpenSim.Framework.Communications.Tests TestHelper.InMethod(); Scene myScene = SceneSetupHelpers.SetupScene("inventory"); - CachedUserInfo userInfo = UserProfileTestUtils.CreateUserWithInventory(myScene.CommsManager); - for (int i = 0 ; i < 50 ; i++) - { - if (userInfo.HasReceivedInventory == true) - break; - Thread.Sleep(200); + CachedUserInfo userInfo; + + lock (this) + { + userInfo = UserProfileTestUtils.CreateUserWithInventory(myScene.CommsManager, InventoryReceived); + Monitor.Wait(this, 60000); } - Assert.That(userInfo.HasReceivedInventory, Is.True, "FetchInventory timed out (10 seconds)"); UUID folderId = UUID.Parse("00000000-0000-0000-0000-000000000010"); Assert.That(userInfo.RootFolder.ContainsChildFolder(folderId), Is.False); @@ -192,14 +203,13 @@ namespace OpenSim.Framework.Communications.Tests TestHelper.InMethod(); Scene myScene = SceneSetupHelpers.SetupScene("inventory"); - CachedUserInfo userInfo = UserProfileTestUtils.CreateUserWithInventory(myScene.CommsManager); - for (int i = 0 ; i < 50 ; i++) - { - if (userInfo.HasReceivedInventory == true) - break; - Thread.Sleep(200); + CachedUserInfo userInfo; + + lock (this) + { + userInfo = UserProfileTestUtils.CreateUserWithInventory(myScene.CommsManager, InventoryReceived); + Monitor.Wait(this, 60000); } - Assert.That(userInfo.HasReceivedInventory, Is.True, "FetchInventory timed out (10 seconds)"); UUID folder1Id = UUID.Parse("00000000-0000-0000-0000-000000000060"); InventoryFolderImpl rootFolder = userInfo.RootFolder; @@ -258,14 +268,13 @@ namespace OpenSim.Framework.Communications.Tests TestHelper.InMethod(); Scene myScene = SceneSetupHelpers.SetupScene("inventory"); - CachedUserInfo userInfo = UserProfileTestUtils.CreateUserWithInventory(myScene.CommsManager); - for (int i = 0 ; i < 50 ; i++) - { - if (userInfo.HasReceivedInventory == true) - break; - Thread.Sleep(200); + CachedUserInfo userInfo; + + lock (this) + { + userInfo = UserProfileTestUtils.CreateUserWithInventory(myScene.CommsManager, InventoryReceived); + Monitor.Wait(this, 60000); } - Assert.That(userInfo.HasReceivedInventory, Is.True, "FetchInventory timed out (10 seconds)"); UUID folder1Id = UUID.Parse("00000000-0000-0000-0000-000000000020"); UUID folder2Id = UUID.Parse("00000000-0000-0000-0000-000000000021"); @@ -299,14 +308,13 @@ namespace OpenSim.Framework.Communications.Tests //log4net.Config.XmlConfigurator.Configure(); Scene myScene = SceneSetupHelpers.SetupScene("inventory"); - CachedUserInfo userInfo = UserProfileTestUtils.CreateUserWithInventory(myScene.CommsManager); - for (int i = 0 ; i < 50 ; i++) - { - if (userInfo.HasReceivedInventory == true) - break; - Thread.Sleep(200); + CachedUserInfo userInfo; + + lock (this) + { + userInfo = UserProfileTestUtils.CreateUserWithInventory(myScene.CommsManager, InventoryReceived); + Monitor.Wait(this, 60000); } - Assert.That(userInfo.HasReceivedInventory, Is.True, "FetchInventory timed out (10 seconds)"); UUID folder1Id = UUID.Parse("00000000-0000-0000-0000-000000000070"); InventoryFolderImpl rootFolder = userInfo.RootFolder; @@ -323,4 +331,4 @@ namespace OpenSim.Framework.Communications.Tests Assert.That(myScene.InventoryService.QueryFolder(myFolder), Is.Null); } } -} +} \ No newline at end of file -- cgit v1.1