Establish CachedUserInfo.OnInventoryReceived event so that region/test inventory code can be written with the async inventory fetch

arthursv
Justin Clark-Casey (justincc) 2009-08-11 16:45:16 +01:00
parent 2054093908
commit 226c082ed4
4 changed files with 140 additions and 63 deletions

View File

@ -49,16 +49,24 @@ namespace OpenSim.Framework.Communications.Cache
IClientAPI client, UUID folderID, bool fetchFolders, bool fetchItems); IClientAPI client, UUID folderID, bool fetchFolders, bool fetchItems);
public delegate void OnItemReceivedDelegate(UUID itemID); public delegate void OnItemReceivedDelegate(UUID itemID);
public delegate void OnInventoryReceivedDelegate(UUID userID);
/// <summary> /// <summary>
/// Stores user profile and inventory data received from backend services for a particular user. /// Stores user profile and inventory data received from backend services for a particular user.
/// </summary> /// </summary>
public class CachedUserInfo public class CachedUserInfo
{ {
private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
//// <value>
/// Fired when a particular item has been received from the inventory service
/// </value>
public event OnItemReceivedDelegate OnItemReceived; public event OnItemReceivedDelegate OnItemReceived;
private static readonly ILog m_log /// <value>
= LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); /// Fired once the entire inventory has been received for the user
/// </value>
public event OnInventoryReceivedDelegate OnInventoryReceived;
/// <summary> /// <summary>
/// The comms manager holds references to services (user, grid, inventory, etc.) /// The comms manager holds references to services (user, grid, inventory, etc.)
@ -133,7 +141,9 @@ namespace OpenSim.Framework.Communications.Cache
UUID parentFolderId = folder.ParentID; UUID parentFolderId = folder.ParentID;
if (dictionary.ContainsKey(parentFolderId)) if (dictionary.ContainsKey(parentFolderId))
{
dictionary[parentFolderId].Add(folder); dictionary[parentFolderId].Add(folder);
}
else else
{ {
IList<InventoryFolderImpl> folders = new List<InventoryFolderImpl>(); IList<InventoryFolderImpl> folders = new List<InventoryFolderImpl>();
@ -299,6 +309,9 @@ namespace OpenSim.Framework.Communications.Cache
request.Execute(); request.Execute();
} }
} }
if (OnInventoryReceived != null)
OnInventoryReceived(UserProfile.ID);
} }
/// <summary> /// <summary>

View File

@ -43,6 +43,18 @@ namespace OpenSim.Framework.Communications.Tests
[TestFixture] [TestFixture]
public class UserProfileCacheServiceTests public class UserProfileCacheServiceTests
{ {
/// <value>Used by tests to indicate whether an async operation timed out</value>
private bool timedOut;
private void InventoryReceived(UUID userId)
{
lock (this)
{
timedOut = false;
Monitor.PulseAll(this);
}
}
[Test] [Test]
public void TestGetUserDetails() public void TestGetUserDetails()
{ {
@ -118,14 +130,15 @@ namespace OpenSim.Framework.Communications.Tests
TestHelper.InMethod(); TestHelper.InMethod();
Scene myScene = SceneSetupHelpers.SetupScene("inventory"); Scene myScene = SceneSetupHelpers.SetupScene("inventory");
CachedUserInfo userInfo = UserProfileTestUtils.CreateUserWithInventory(myScene.CommsManager);
for (int i = 0 ; i < 50 ; i++) timedOut = true;
lock (this)
{ {
if (userInfo.HasReceivedInventory == true) UserProfileTestUtils.CreateUserWithInventory(myScene.CommsManager, InventoryReceived);
break; Monitor.Wait(this, 60000);
Thread.Sleep(200);
} }
Assert.That(userInfo.HasReceivedInventory, Is.True, "FetchInventory timed out (10 seconds)");
Assert.That(timedOut, Is.False, "Timed out");
} }
[Test] [Test]
@ -134,14 +147,13 @@ namespace OpenSim.Framework.Communications.Tests
TestHelper.InMethod(); TestHelper.InMethod();
Scene myScene = SceneSetupHelpers.SetupScene("inventory"); Scene myScene = SceneSetupHelpers.SetupScene("inventory");
CachedUserInfo userInfo = UserProfileTestUtils.CreateUserWithInventory(myScene.CommsManager); CachedUserInfo userInfo;
for (int i = 0 ; i < 50 ; i++)
lock (this)
{ {
if (userInfo.HasReceivedInventory == true) userInfo = UserProfileTestUtils.CreateUserWithInventory(myScene.CommsManager, InventoryReceived);
break; Monitor.Wait(this, 60000);
Thread.Sleep(200);
} }
Assert.That(userInfo.HasReceivedInventory, Is.True, "FetchInventory timed out (10 seconds)");
UUID folderId = UUID.Parse("00000000-0000-0000-0000-000000000011"); UUID folderId = UUID.Parse("00000000-0000-0000-0000-000000000011");
Assert.That(userInfo.RootFolder.GetChildFolder(folderId), Is.Null); Assert.That(userInfo.RootFolder.GetChildFolder(folderId), Is.Null);
@ -156,14 +168,13 @@ namespace OpenSim.Framework.Communications.Tests
TestHelper.InMethod(); TestHelper.InMethod();
Scene myScene = SceneSetupHelpers.SetupScene("inventory"); Scene myScene = SceneSetupHelpers.SetupScene("inventory");
CachedUserInfo userInfo = UserProfileTestUtils.CreateUserWithInventory(myScene.CommsManager); CachedUserInfo userInfo;
for (int i = 0 ; i < 50 ; i++)
lock (this)
{ {
if (userInfo.HasReceivedInventory == true) userInfo = UserProfileTestUtils.CreateUserWithInventory(myScene.CommsManager, InventoryReceived);
break; Monitor.Wait(this, 60000);
Thread.Sleep(200);
} }
Assert.That(userInfo.HasReceivedInventory, Is.True, "FetchInventory timed out (10 seconds)");
UUID folderId = UUID.Parse("00000000-0000-0000-0000-000000000010"); UUID folderId = UUID.Parse("00000000-0000-0000-0000-000000000010");
Assert.That(userInfo.RootFolder.ContainsChildFolder(folderId), Is.False); Assert.That(userInfo.RootFolder.ContainsChildFolder(folderId), Is.False);
@ -192,14 +203,13 @@ namespace OpenSim.Framework.Communications.Tests
TestHelper.InMethod(); TestHelper.InMethod();
Scene myScene = SceneSetupHelpers.SetupScene("inventory"); Scene myScene = SceneSetupHelpers.SetupScene("inventory");
CachedUserInfo userInfo = UserProfileTestUtils.CreateUserWithInventory(myScene.CommsManager); CachedUserInfo userInfo;
for (int i = 0 ; i < 50 ; i++)
lock (this)
{ {
if (userInfo.HasReceivedInventory == true) userInfo = UserProfileTestUtils.CreateUserWithInventory(myScene.CommsManager, InventoryReceived);
break; Monitor.Wait(this, 60000);
Thread.Sleep(200);
} }
Assert.That(userInfo.HasReceivedInventory, Is.True, "FetchInventory timed out (10 seconds)");
UUID folder1Id = UUID.Parse("00000000-0000-0000-0000-000000000060"); UUID folder1Id = UUID.Parse("00000000-0000-0000-0000-000000000060");
InventoryFolderImpl rootFolder = userInfo.RootFolder; InventoryFolderImpl rootFolder = userInfo.RootFolder;
@ -258,14 +268,13 @@ namespace OpenSim.Framework.Communications.Tests
TestHelper.InMethod(); TestHelper.InMethod();
Scene myScene = SceneSetupHelpers.SetupScene("inventory"); Scene myScene = SceneSetupHelpers.SetupScene("inventory");
CachedUserInfo userInfo = UserProfileTestUtils.CreateUserWithInventory(myScene.CommsManager); CachedUserInfo userInfo;
for (int i = 0 ; i < 50 ; i++)
lock (this)
{ {
if (userInfo.HasReceivedInventory == true) userInfo = UserProfileTestUtils.CreateUserWithInventory(myScene.CommsManager, InventoryReceived);
break; Monitor.Wait(this, 60000);
Thread.Sleep(200);
} }
Assert.That(userInfo.HasReceivedInventory, Is.True, "FetchInventory timed out (10 seconds)");
UUID folder1Id = UUID.Parse("00000000-0000-0000-0000-000000000020"); UUID folder1Id = UUID.Parse("00000000-0000-0000-0000-000000000020");
UUID folder2Id = UUID.Parse("00000000-0000-0000-0000-000000000021"); UUID folder2Id = UUID.Parse("00000000-0000-0000-0000-000000000021");
@ -299,14 +308,13 @@ namespace OpenSim.Framework.Communications.Tests
//log4net.Config.XmlConfigurator.Configure(); //log4net.Config.XmlConfigurator.Configure();
Scene myScene = SceneSetupHelpers.SetupScene("inventory"); Scene myScene = SceneSetupHelpers.SetupScene("inventory");
CachedUserInfo userInfo = UserProfileTestUtils.CreateUserWithInventory(myScene.CommsManager); CachedUserInfo userInfo;
for (int i = 0 ; i < 50 ; i++)
lock (this)
{ {
if (userInfo.HasReceivedInventory == true) userInfo = UserProfileTestUtils.CreateUserWithInventory(myScene.CommsManager, InventoryReceived);
break; Monitor.Wait(this, 60000);
Thread.Sleep(200);
} }
Assert.That(userInfo.HasReceivedInventory, Is.True, "FetchInventory timed out (10 seconds)");
UUID folder1Id = UUID.Parse("00000000-0000-0000-0000-000000000070"); UUID folder1Id = UUID.Parse("00000000-0000-0000-0000-000000000070");
InventoryFolderImpl rootFolder = userInfo.RootFolder; InventoryFolderImpl rootFolder = userInfo.RootFolder;

View File

@ -54,6 +54,14 @@ namespace OpenSim.Region.CoreModules.Avatar.Inventory.Archiver.Tests
[TestFixture] [TestFixture]
public class InventoryArchiverTests public class InventoryArchiverTests
{ {
private void InventoryReceived(UUID userId)
{
lock (this)
{
Monitor.PulseAll(this);
}
}
private void SaveCompleted( private void SaveCompleted(
bool succeeded, CachedUserInfo userInfo, string invPath, Stream saveStream, Exception reportedException) bool succeeded, CachedUserInfo userInfo, string invPath, Stream saveStream, Exception reportedException)
{ {
@ -82,8 +90,19 @@ namespace OpenSim.Region.CoreModules.Avatar.Inventory.Archiver.Tests
string userFirstName = "Jock"; string userFirstName = "Jock";
string userLastName = "Stirrup"; string userLastName = "Stirrup";
UUID userId = UUID.Parse("00000000-0000-0000-0000-000000000020"); UUID userId = UUID.Parse("00000000-0000-0000-0000-000000000020");
CachedUserInfo userInfo;
lock (this)
{
userInfo
= UserProfileTestUtils.CreateUserWithInventory(
cm, userFirstName, userLastName, userId, InventoryReceived);
Monitor.Wait(this, 60000);
}
/*
cm.UserAdminService.AddUser(userFirstName, userLastName, string.Empty, string.Empty, 1000, 1000, userId); cm.UserAdminService.AddUser(userFirstName, userLastName, string.Empty, string.Empty, 1000, 1000, userId);
CachedUserInfo userInfo = cm.UserProfileCacheService.GetUserDetails(userId); CachedUserInfo userInfo = cm.UserProfileCacheService.GetUserDetails(userId, InventoryReceived);
userInfo.FetchInventory(); userInfo.FetchInventory();
for (int i = 0 ; i < 50 ; i++) for (int i = 0 ; i < 50 ; i++)
{ {
@ -92,6 +111,7 @@ namespace OpenSim.Region.CoreModules.Avatar.Inventory.Archiver.Tests
Thread.Sleep(200); Thread.Sleep(200);
} }
Assert.That(userInfo.HasReceivedInventory, Is.True, "FetchInventory timed out (10 seconds)"); Assert.That(userInfo.HasReceivedInventory, Is.True, "FetchInventory timed out (10 seconds)");
*/
// Create asset // Create asset
SceneObjectGroup object1; SceneObjectGroup object1;
@ -338,14 +358,14 @@ namespace OpenSim.Region.CoreModules.Avatar.Inventory.Archiver.Tests
CachedUserInfo userInfo CachedUserInfo userInfo
= scene.CommsManager.UserProfileCacheService.GetUserDetails(userFirstName, userLastName); = scene.CommsManager.UserProfileCacheService.GetUserDetails(userFirstName, userLastName);
userInfo.FetchInventory(); userInfo.OnInventoryReceived += InventoryReceived;
for (int i = 0 ; i < 50 ; i++)
lock (this)
{ {
if (userInfo.HasReceivedInventory == true) userInfo.FetchInventory();
break; Monitor.Wait(this, 60000);
Thread.Sleep(200);
} }
Assert.That(userInfo.HasReceivedInventory, Is.True, "FetchInventory timed out (10 seconds)");
InventoryItemBase foundItem = userInfo.RootFolder.FindItemByPath(itemName); InventoryItemBase foundItem = userInfo.RootFolder.FindItemByPath(itemName);
Assert.That(foundItem.CreatorId, Is.EqualTo(item1.CreatorId)); Assert.That(foundItem.CreatorId, Is.EqualTo(item1.CreatorId));
@ -365,8 +385,14 @@ namespace OpenSim.Region.CoreModules.Avatar.Inventory.Archiver.Tests
TestHelper.InMethod(); TestHelper.InMethod();
Scene scene = SceneSetupHelpers.SetupScene(""); Scene scene = SceneSetupHelpers.SetupScene("");
CommunicationsManager commsManager = scene.CommsManager; CommunicationsManager commsManager = scene.CommsManager;
CachedUserInfo userInfo;
lock (this)
{
userInfo = UserProfileTestUtils.CreateUserWithInventory(commsManager, InventoryReceived);
Monitor.Wait(this, 60000);
}
CachedUserInfo userInfo = UserProfileTestUtils.CreateUserWithInventory(commsManager);
//userInfo.FetchInventory(); //userInfo.FetchInventory();
/* /*
for (int i = 0 ; i < 50 ; i++) for (int i = 0 ; i < 50 ; i++)

View File

@ -41,25 +41,55 @@ namespace OpenSim.Tests.Common.Setup
/// Create a test user with a standard inventory /// Create a test user with a standard inventory
/// </summary> /// </summary>
/// <param name="commsManager"></param> /// <param name="commsManager"></param>
/// <param name="callback">
/// Callback to invoke when inventory has been loaded. This is required because
/// loading may be asynchronous, even on standalone
/// </param>
/// <returns></returns> /// <returns></returns>
public static CachedUserInfo CreateUserWithInventory(CommunicationsManager commsManager) public static CachedUserInfo CreateUserWithInventory(
CommunicationsManager commsManager, OnInventoryReceivedDelegate callback)
{ {
UUID userId = UUID.Parse("00000000-0000-0000-0000-000000000099"); UUID userId = UUID.Parse("00000000-0000-0000-0000-000000000099");
return CreateUserWithInventory(commsManager, userId); return CreateUserWithInventory(commsManager, userId, callback);
} }
/// <summary> /// <summary>
/// Create a test user with a standard inventory /// Create a test user with a standard inventory
/// </summary> /// </summary>
/// <param name="commsManager"></param> /// <param name="commsManager"></param>
/// <param name="userId">Explicit user id to use for user creation</param> /// <param name="userId">User ID</param>
/// <param name="callback">
/// Callback to invoke when inventory has been loaded. This is required because
/// loading may be asynchronous, even on standalone
/// </param>
/// <returns></returns> /// <returns></returns>
public static CachedUserInfo CreateUserWithInventory(CommunicationsManager commsManager, UUID userId) public static CachedUserInfo CreateUserWithInventory(
CommunicationsManager commsManager, UUID userId, OnInventoryReceivedDelegate callback)
{
return CreateUserWithInventory(commsManager, "Bill", "Bailey", userId, callback);
}
/// <summary>
/// Create a test user with a standard inventory
/// </summary>
/// <param name="commsManager"></param>
/// <param name="firstName">First name of user</param>
/// <param name="lastName">Last name of user</param>
/// <param name="userId">User ID</param>
/// <param name="callback">
/// Callback to invoke when inventory has been loaded. This is required because
/// loading may be asynchronous, even on standalone
/// </param>
/// <returns></returns>
public static CachedUserInfo CreateUserWithInventory(
CommunicationsManager commsManager, string firstName, string lastName,
UUID userId, OnInventoryReceivedDelegate callback)
{ {
LocalUserServices lus = (LocalUserServices)commsManager.UserService; LocalUserServices lus = (LocalUserServices)commsManager.UserService;
lus.AddUser("Bill", "Bailey", "troll", "bill@bailey.com", 1000, 1000, userId); lus.AddUser(firstName, lastName, "troll", "bill@bailey.com", 1000, 1000, userId);
CachedUserInfo userInfo = commsManager.UserProfileCacheService.GetUserDetails(userId); CachedUserInfo userInfo = commsManager.UserProfileCacheService.GetUserDetails(userId);
userInfo.OnInventoryReceived += callback;
userInfo.FetchInventory(); userInfo.FetchInventory();
return userInfo; return userInfo;