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);
public delegate void OnItemReceivedDelegate(UUID itemID);
public delegate void OnInventoryReceivedDelegate(UUID userID);
/// <summary>
/// Stores user profile and inventory data received from backend services for a particular user.
/// </summary>
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;
private static readonly ILog m_log
= LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
/// <value>
/// Fired once the entire inventory has been received for the user
/// </value>
public event OnInventoryReceivedDelegate OnInventoryReceived;
/// <summary>
/// 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<InventoryFolderImpl> folders = new List<InventoryFolderImpl>();
@ -299,6 +309,9 @@ namespace OpenSim.Framework.Communications.Cache
request.Execute();
}
}
if (OnInventoryReceived != null)
OnInventoryReceived(UserProfile.ID);
}
/// <summary>

View File

@ -43,6 +43,18 @@ namespace OpenSim.Framework.Communications.Tests
[TestFixture]
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]
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++)
timedOut = true;
lock (this)
{
if (userInfo.HasReceivedInventory == true)
break;
Thread.Sleep(200);
}
Assert.That(userInfo.HasReceivedInventory, Is.True, "FetchInventory timed out (10 seconds)");
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++)
CachedUserInfo userInfo;
lock (this)
{
if (userInfo.HasReceivedInventory == true)
break;
Thread.Sleep(200);
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++)
CachedUserInfo userInfo;
lock (this)
{
if (userInfo.HasReceivedInventory == true)
break;
Thread.Sleep(200);
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++)
CachedUserInfo userInfo;
lock (this)
{
if (userInfo.HasReceivedInventory == true)
break;
Thread.Sleep(200);
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++)
CachedUserInfo userInfo;
lock (this)
{
if (userInfo.HasReceivedInventory == true)
break;
Thread.Sleep(200);
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++)
CachedUserInfo userInfo;
lock (this)
{
if (userInfo.HasReceivedInventory == true)
break;
Thread.Sleep(200);
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;

View File

@ -54,6 +54,14 @@ namespace OpenSim.Region.CoreModules.Avatar.Inventory.Archiver.Tests
[TestFixture]
public class InventoryArchiverTests
{
private void InventoryReceived(UUID userId)
{
lock (this)
{
Monitor.PulseAll(this);
}
}
private void SaveCompleted(
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 userLastName = "Stirrup";
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);
CachedUserInfo userInfo = cm.UserProfileCacheService.GetUserDetails(userId);
CachedUserInfo userInfo = cm.UserProfileCacheService.GetUserDetails(userId, InventoryReceived);
userInfo.FetchInventory();
for (int i = 0 ; i < 50 ; i++)
{
@ -92,6 +111,7 @@ namespace OpenSim.Region.CoreModules.Avatar.Inventory.Archiver.Tests
Thread.Sleep(200);
}
Assert.That(userInfo.HasReceivedInventory, Is.True, "FetchInventory timed out (10 seconds)");
*/
// Create asset
SceneObjectGroup object1;
@ -338,14 +358,14 @@ namespace OpenSim.Region.CoreModules.Avatar.Inventory.Archiver.Tests
CachedUserInfo userInfo
= scene.CommsManager.UserProfileCacheService.GetUserDetails(userFirstName, userLastName);
userInfo.FetchInventory();
for (int i = 0 ; i < 50 ; i++)
userInfo.OnInventoryReceived += InventoryReceived;
lock (this)
{
if (userInfo.HasReceivedInventory == true)
break;
Thread.Sleep(200);
userInfo.FetchInventory();
Monitor.Wait(this, 60000);
}
Assert.That(userInfo.HasReceivedInventory, Is.True, "FetchInventory timed out (10 seconds)");
InventoryItemBase foundItem = userInfo.RootFolder.FindItemByPath(itemName);
Assert.That(foundItem.CreatorId, Is.EqualTo(item1.CreatorId));
@ -365,8 +385,14 @@ namespace OpenSim.Region.CoreModules.Avatar.Inventory.Archiver.Tests
TestHelper.InMethod();
Scene scene = SceneSetupHelpers.SetupScene("");
CommunicationsManager commsManager = scene.CommsManager;
CachedUserInfo userInfo;
lock (this)
{
userInfo = UserProfileTestUtils.CreateUserWithInventory(commsManager, InventoryReceived);
Monitor.Wait(this, 60000);
}
CachedUserInfo userInfo = UserProfileTestUtils.CreateUserWithInventory(commsManager);
//userInfo.FetchInventory();
/*
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
/// </summary>
/// <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>
public static CachedUserInfo CreateUserWithInventory(CommunicationsManager commsManager)
public static CachedUserInfo CreateUserWithInventory(
CommunicationsManager commsManager, OnInventoryReceivedDelegate callback)
{
UUID userId = UUID.Parse("00000000-0000-0000-0000-000000000099");
return CreateUserWithInventory(commsManager, userId);
return CreateUserWithInventory(commsManager, userId, callback);
}
/// <summary>
/// Create a test user with a standard inventory
/// </summary>
/// <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>
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;
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);
userInfo.OnInventoryReceived += callback;
userInfo.FetchInventory();
return userInfo;