Rational Functional Tester - How can I get scripts called from a parent script to use the parent's data pool? - automated-tests

I'm fairly new to Rational Functional Tester (Java) but I have one large blank. I have an application that is in an agile development environment so some of the screens can flux as new interfaces are brought online.
For this reason I'm trying to modularize my test scripts. For example: I would like to have a login script, a search script, and a logout script.
I would then stitch these together (pseudo code)
Call Script components.security.Login;
Call Script components.search.Search;
//verification point
Call Script components.security.Logout;
By breaking the testing script into discrete chunks (functional units) I believe that I would be better able to adapt to change. If the login script changed, I would fix or re-record it once for every script in the application.
Then I would call that script, say, "TestSituation_001". It would have need to refer to several different data pools. In this instance a User datapool (instead of a superUser datapool) and a TestSituation_001 datapool, or possibly some other datapools as well. The verfication point would use the situational datapool for its check.
Now, this is how I would do it in an ideal world. What is bothering me at the moment is that it appears that I would need to do something entirely different in order to get the child scripts to inherit the parents.
So my questions are these:
Why don't child scripts just inherit the calling script's data pool?
How can I make them do it?
Am I making poor assumptions about the way this should work?
If #3 is true, then how can I do better?
As a side note, I don't mind hacking the heck out of some Java to make it work.
Thanks!

I solved my own problem. For those of you who are curious, check this out:
public abstract class MyTestHelper extends RationalTestScript
{
protected void useParentDataPool() {
if(this.getScriptCaller() != null) {
IDatapool dp = this.getScriptCaller().getDatapool();
IDatapoolIterator iterator = DatapoolFactory.get().open(dp, "");
if(dp != null && iterator != null) {
//if the datapool is not null, substitute it for the current data pool
this.dpInitialization(dp, iterator);
}
}
}
}
This will use the same iterator too. Happy hunting...
Actually, after some reflection, I made a method that would make any given script use the Root calling script's DataPool. Again, happy hunting to those who need it...
/*
* preconditions: there is a parent caller
* postconditions: the current script is now using the same datapool / datapool iterator as the root script
*/
protected void useRootDataPool() {
//if there is no parent, then this wouldn't work so return with no result;
if(this.getScriptCaller() == null) return;
//assume that we're at the root node to start
RationalTestScript root = this;
while(root.getScriptCaller() != null) {
root = root.getScriptCaller();
}
//if this node is the root node, no need to continue. the default attached datapool will suffice.
if(this.equals(root)) return;
//get the root's data pool (which would be the parent's parent and so on to the topmost)
IDatapool dp = root.getDatapool();
if(dp != null) {
//check to make sure that we're not trying to re-initialize with the same datapool (by name)
//if we are, then leave
if(dp.getName().equals(this.getDatapool().getName())) return;
//this basically says "give me the iterator already associated to this pool"
IDatapoolIterator iterator = DatapoolFactory.get().open(dp, "");
//if we have an iterator AND a data pool (from above), then we can initialize
if(iterator != null) {
//this method is never supposed to be run, but this works just fine.
this.dpInitialization(dp, iterator);
//log information
logInfo("Using data pool from root script: " + root.getScriptName());
}
}
}

Related

Binary Search Tree Insert Implementation

So my question is as follows:
When I run the code for this insert helper method, and I am positive my new node method is correct as it works for instantiating a Binary Search Tree, no nodes are inserted. Why can't I use this certain implementation? What's going wrong here?
I know how to use the other insert implementation where one would check for the left and right nodes of the root and whether or not they are null, but can not figure out the problem of this more elegant possibility. The answer to this will help me in creating other functions that go beyond the scope of the insert function.
btw yes I have another function calling this helper function
Thanks!!!!!
//INSERT METHODS
void BinarySearchTree::insert(int data, struct node* root) {
//If root is null make new node there
if (!root) {
root = new node(data);
}
else if (root -> data > data) {
insert(data, root -> left);
}
else {
insert(data, root -> right);
}
}
The variable root is a parameter, which only has local visibility for that one method call. Meaning root = new node(data) will indeed create a new node, but that will only be pointed to by the parameter. Your method doesn't return anything and it doesn't actually know what it is supposed to do with that new root object of yours (it is NOT the same as any class variable you might have defined that is named the same).
So you create a new node, but can't use it outside that one method call. Which results in an empty tree.
As a side note for future questions: Include a tag for the programming language you are using. A lot of people use that as a filter, so you will actually get more people looking at this if you use the right tag.

I can't catch an Error

I can't catch the thrown error in my simplified code below. Why is that?
According to requirements of the stackoverflow I must insert some more info but this example is very simple. Can you help me with this example?
package com.myserver {
public class ReturnInfo extends Sprite {
public function ReturnInfo(urlParamsArr:Array) {
try {
var client:HttpClient = new HttpClient();
var uri:URI = new URI("http://valid-url.com/aaa.php");
client.listener.onData = function(event:HttpDataEvent):void {
throw new Error();
};
client.listener.onError = function(event:IOErrorEvent):void {
trace("error");
};
client.postFormData(uri, variables);
}
catch (e:Error){
trace("Error was caught.");
}
}
} //class
} //package
I tried also:
try {
new ReturnInfo(urlParamsArr);
}
catch(e:Error){
trace("caught error");
}
It didn't work either.
The code does not work because the code that throws error is executed later, so you need to use try-catch in the client.listener.onData handler. That handler I assume is called sometimes later so there when you parse or handle the data,make sure to catch/handle the errors
Adding on to what Simion said, the problem is method closure. In order for an exception to be caught somewhere in the "food chain" the catch needs to be in the stack - you will know what is in the current stack by getStackTrace(). In this example, there is no stack pointer that sits at the constructor (or any method) like there is one for client.listener.onData - which is why the postFormData will execute. When the event is triggered it's stack pointer goes back to the origination point of what actually started the event trigger in the first place (not the method that declared it). This is also why the 2nd attempt was unsuccessful.
Add on to the fact that the FP executes discrete chunks in frames (think of this like a heap), anything that executes in the scope of the dispatchEvent will generally have a very small or no stack at all (eg the first stack pointer is usually the dispatcher itself - not a method that actually called it).
try-catch is best attempted within the same scope of a method.
A pseudo example:
function getOrCreateWidget():Widget {
var a:Widget;
try {
a = getWidet();
}
catch(e:TypeError) {
a = createNewWidget();
}
//finally can be debatable - most of us leave it off
//bc it executes anyway just as it would in the function scope.
finally {
a.property = 'foo';
}
return a;
}
If this isn't possible - a last ditch effort is to attach a listener to the loaderInfo.uncaughtErrorEvents. Generally associating this with the systemManager is the best option because the SM knows about every branch of the display tree right down to the root stage. It's neither good practice nor practical to assign all deviations in this method because a lot of context to the programmer is usually lost. It's more an "oh S#!) sorry user, our app just verped."

Am I using nHIbernate's ITransaction properly here?

I'm just learning nHibernate and have come across what probably is a simple issue to resolve.
Right so I've figured out so far that you can't/shouldn;t nest nHibernate Transactions within each other; in my case I figured this out when scope went to another routine and I started a new Transaction.
So should I be doing the following?
using (ITransaction transaction = session.BeginTransaction())
{
NHibernateMembership mQuery =
session.QueryOver<NHibernateMembership>()
.Where(x => x.Username == username)
.And(x => x.ApplicationName == ApplicationName)
.SingleOrDefault();
if (mQuery != null)
{
mQuery.PasswordQuestion = newPwdQuestion;
mQuery.PasswordAnswer = EncodePassword(newPwdAnswer);
session.Update(mQuery);
transaction.Commit();
passwordQuestionUpdated = true;
}
}
// Assume this is in another routine elsewhere but being
// called right after the first in the same request
using (ITransaction transaction = session.BeginTransaction())
{
NHibernateMembership mQuery =
session.QueryOver<NHibernateMembership>()
.Where(x => x.Username == username)
.And(x => x.ApplicationName == ApplicationName)
.SingleOrDefault();
if (mQuery != null)
{
mQuery.PasswordQuestion = newPwdQuestion;
mQuery.PasswordAnswer = EncodePassword(newPwdAnswer);
session.Update(mQuery);
transaction.Commit();
passwordQuestionUpdated = true;
}
}
Note: I know they are simply a copy, i'm just demonstrating my question
First Question
Is this the way it is MEANT to be done? Transaction per operation?
Second Question
Do I need call transaction.Commit(); each time or only in the last set?
Third Question
Is there a better way, automated or manual, to do this?
Third Question
Can I use session.Transaction.IsActive to determine if the "Current Session" already is part of a transaction - so in this case I can make the "Transaction wrap" in the highest level, let's say the Web Form code, and let routines be called within it and then commit all work at the end. Is this a flawed method?
I really want to hammer this down so I start as I mean to go on; I don;t want to find 1000s of lines of code in that I need to change it all.
Thanks in advance.
EDIT:
Right so I wrote some code to explain my issue exactly.
private void CallingRoutine()
{
using(ISession session = Helper.GetCurrentSession)
{
using (ITransaction transaction = session.BeginTransaction())
{
// RUN nHIbernate QUERY to get an OBJECT-1
// DO WORK on OBJECT
// Need to CALL an EXTERNAL ROUTINE to finish work
ExternalRoutine();
// DO WORK on OBJECT-1 again
// *** At this point ADO exception triggers
}
}
}
private bool ExternalRoutine()
{
using(ISession session = Helper.GetCurrentSession)
{
using (ITransaction transaction = session.BeginTransaction())
{
// RUN nHIbernate QUERY to get an OBJECT-2
// DO WORK on OBJECT
// Determine result
if(Data)
{
return true;
}
return false;
}
}
}
Hopefully this demonstrates the issue. This is how I understood to write the Transactions but notice how the ADO exception occurs. I'm obviously doing something wrong. How am I meant to write these routines?
Take for example if I was to write a helper object for some provider and within each routine exposed there is a nHibernate query run - how wold I write those routines, in regards to Transactions, assuming I knew nothing about the calling function and data - my job is to work with nHibernate effectively and efficiently and return a result.
This is what I assumed by writing the transaction how I did in ExternalRoutine() - to assume that this is the only use of nHibernate and to explicitly make the Transaction.
If possible, I would suggest using System.Transactions.TransactionScope:
using(var trans = new TransactionScope())
using(var session = .. create your session...) {
... do your stuff ...
trans.Complete();
}
The trans.Complete will cause the session to flush and commit the transaction, in addition you can have nested transactionscopes. The only "downside" to this is that it will escalate to DTC if you have multiple connections (or enlisted resources such as MSMQ), but this is not necessarily a downside unless you're using something like MySQL which doesn't play nicely with DTC.
For simple cases I would probably use a transaction scope in the BeginRequest and commit it in EndRequest if there were no errors (or use a filter if u're using ASP.NET MVC), but that really depends a lot on what you're doing - as long as your operations are short (which they should be in a web app), this should be fine. The overhead of creating a session and transaction scope is not that big that it should cause problems for accessing static resources (or resources that don't need the session), unless you're looking at a really high volume / high performance website.

Flash Player: Get reference count for variable

I'm looking to build a library that needs to be very careful about memory management. Basically, I have to create a static factory to "disperse" instances of my tool to requesting objects. (I don't have a choice in this matter, I really do have to use a singleton) We'll call that class FooFactory. FooFactory defines a single method, getFoo(key:String):Foo.
getFoo looks in a private static flash.utils.Dictionary object for the appropriate Foo instance, and either lazy-instantiates it, or simply returns it. In any case, FooFactory MUST keep a reference to each Foo instance created, so all Foo instances can be updated by FooFactory using a method called updateFoos():void.
Here is some pseudo-code of what I'm talking about:
public class FooFactory {
private static const foos:Dictionary = new Dictionary(true); //use weak keys for gc
public static function getFoo(key:String):Foo {
//search for the specified instance in the 'foos' dictionary
if (foos[key] != null && foos[key] != undefined) {
return foos[key];
} else {
//create foo if it doesn't exist.
var foo:Foo = new Foo(key);
foos[key] = foo;
return foo;
}
}
public static function updateFoos():void {
for (var key:String in foos) {
if (foos[key] != null && foos[key] != undefined) {
Foo(foos[key]).dispatchEvent(new Event("update"));
}
}
}
}
The actual function and identity of Foo isn't too important.
What IS important is garbage collection in this situation. I created something similar to the above example in the past and had incredible garbage collection issues. (I did use an array rather than a dictionary, which could be part of the problem.) What would happen is that, in my Flex application, modules would never unload, since instances had a reference to a Foo instance which was referenced by the FooFactory, like so: (again, pseudocode)
<?xml version="1.0"?>
<s:Group>
<fx:Script>
<![CDATA[
private static const foo:Foo = FooFactory.getFoo('myfoo');
]]>
</fx:Script>
</s:Group>
What I want to know are the two following things:
Is the pseudo-code above "garbage-collector safe?" IE: Will my modules unload properly and will instances of the Group subclass above get garbage collected?
Is there a way in Flash Player (even in the debug player if need be) that can assist me in counting references so I can test if things are getting garbage collected or not?
I'm aware of the flash.sampler API, but I am not sure as to how to use it to count references.
I don't think that the pattern you presented should give you problems GC-wise.
private static const foo:Foo = FooFactory.getFoo('myfoo');
Here, your module has a reference to a Foo instance. That means that this Foo instance won't be collectable as long as your module is not collectable. The module has a reference to foo, so here foo is reachable (if the module is reachable). That's not true the other way round. Even if foo lives forever, it doesn't have a reference to the module, so it won't pint it down.
Of course there could be other stuff going on to prevent your module from being collectable, but foo is not the culprit here, unless foo gets a reference to the module somehow. For instance, the module adds a listener to foo, which for this matter, is the same as writing:
foo.addReference(this); // where this is your module
The fact that you declare the instance as const shouldn't change things per se, either. It only means that the reference stored cannot be changed at a later point. However, if you want to null out foo at some later point, you can't because that would be reassigning the reference; and you can't reassigning a const reference (you should get a compiler error). Now, this does tie foo to module. As long as your module is alive it will have a reference to foo, so foo won't be collectable.
Regarding this line:
private static const foos:Dictionary = new Dictionary(true); //use weak keys for gc
It looks like you're trying to build some kind of cache. I'm not sure you want to use weak refs here. (I could be wrong here because I'm making an assumption, and they say assumption is the mother of all... mistakes, but I digress)
In any case, the effect of this is that if a module gets a Foo and at some point the module is successfully unloaded (I mean, cleaned up from memory), that instance of foo could be collected, provided that no one else has a ref to it (that is, the only way to reach it is through the dictionary key, but since the keys are weak referenced, this ref will not count for the purposes of the GC).
Regarding your second question, I'd recommend the FlexBuilder/FlashBuilder profiler, if FB is available to you. It's not the most intuitive tool, granted, but with some practice it could be really useful to track memory problems. Basically, it will let you know how many instances of a given class were created, how many of those are still alive, what objects have references to these instances and where were all these objects allocated (an option not checked by default when you launch the profiler, buy very handy to track a leak).
PS
Regarding your comment:
Perhaps the real issue is the static
const reference bound by the Group
instance? If that's an issue, I could
simply abstract Foo to an interface,
then create something called
FooWeakReference which would use a
weak dictionary to reference the
actual Foo object. Thoughts?
Adding this extra layer of indirection only complicates things and makes your code less obvious for no gain here, I think. It's easier to consider the life-cycle of your module and define clear points of initialization and finalization. When it's finalized, make sure you remove any reference to the module added to the foo instance (i.e. if you have added listeners on foo, remove them, etc), so your module is collectable independently of the life-cycle of foo.
As a general rule, whenever a weak reference seems to solve a bug in your app, it's masking another one or covering up for a poor design; there are exceptions (and compromises that have to be made sometimes), but weak refs are abused gratuitously if you ask me; not everyone will agree, I know.
Also, weak-refs open a whole new kind of bugs: what happens if that instance you created lazily vanishes before you can use it or worse, while you are using it? Event listeners that stop working under not deterministically reproducible circumstances (e.g. you added a listener to an object that is gone), possible null references (e.g. you are trying to add a listener to an object that no longer exists), etc, etc. Don't drink the weak reference kool-aid ;).
Addedum
In conclusion, as one last question,
is it true for me to say that no AS3
solution exists for counting
references? I'm building a complete
unit-testing suite for this library
I'm building, and if I could do
something like Assert.assertEquals(0,
getReferenceCount(foo)), that would be
rad.
Well, yes. You can't get the reference count of a given object from Actionscript. Even if it were possible, I'm not sure that would help, because reference counting is only a part of how GC works. The other one is a mark and sweep algorithm. So, if an object has a zero ref-count is collectable, but it could have, say, 3 references and still be collectable. To really determine whether an object is collectable or not, you should really be able to hook into the GC routine, I guess, and that's not possible from AS.
Also, this code will never work.
Assert.assertEquals(0, getReferenceCount(foo)
Why? Here you are trying to query some API to know whether an object is collectable or not. Since you can't know that, let's assume this tells you whether an object has been collected or not. The problem is, foo at that point is either null or not null. If it's null, it's not a valid reference, so you can't get any useful information out of it, for obvious reasons. If it's not null, it's a valid reference to an object, then you can access it and it's alive; so you already know the answer to the question you're asking.
Now, I think I undestand your goal. You want to be able to tell, programatically, if you certain objects are being leaked. Up to some extent that's possible. It involves using the flash.sampler API, as you mentioned in your original question.
I suggest you check out the Flash Preload Profiler by jpauclair:
I haven't used it, but it looks like it could be just as good as the FB profiler for memory watching.
Since this is Actionscript code (and since it's open source), you could to use it for what you want. I just skimmed through the code, but I've been able to get a very simple-minded proof of concept by monkey-patching the SampleAnalyzer class:
There's a lot of other things going on in this tool, but I just modified the memory analizer to be able to return a list of the alive objects.
So, I wrote a simple class that would run this profiler. The idea is that when you create an object, you can ask this class to watch it. This objects' allocation id will be looked up in the allocated objects table maintained by the memory profiler and a handle to it will be stored locally (only the id). This id handle will also be returned for convenience. So you can store this id handle and at a later point, use it to check whether the object has been collected or not. Also, there's a method that returns a list of all the handles you added and another one that returns a list of the added handles that point to live objects. A handle will allow you to access the original object (if it hasn't been collected yet), its class and also the allocation stack trace. (I'm not storing the object itself or the NewObjectSample object to avoid accidentally pinning it down)
Now, this is important: this queries for alive objects. The fact that an object is alive doesn't mean it's not collectable. So, this alone doens't mean there's a leak. It could be alive at this point but still it doesn't mean there's a leak. So, you should combine this with forcing GC to get more relevant results. Also, this could be of use if you are watching objects that are owned by you and not shared with other code (or other modules).
So, here's the code to the ProfileRunner, with some comments.
import flash.sampler.Sample;
import flash.sampler.NewObjectSample;
import flash.utils.Dictionary;
class ProfilerRunner {
private var _watched:Array;
public function ProfilerRunner() {
_watched = [];
}
public function init():void {
// setup the analyzer. I just copied this almost verbatim
// from SamplerProfiler...
// https://code.google.com/p/flashpreloadprofiler/source/browse/trunk/src/SamplerProfiler.as
SampleAnalyzer.GetInstance().ResetStats();
SampleAnalyzer.GetInstance().ObjectStatsEnabled = true;
SampleAnalyzer.GetInstance().InternalEventStatsEnabled = false;
SampleAnalyzer.GetInstance().StartSampling();
}
public function destroy():void {
_watched = null;
}
private function updateSampling(hook:Function = null):void {
SampleAnalyzer.GetInstance().PauseSampling();
SampleAnalyzer.GetInstance().ProcessSampling();
if(hook is Function) {
var samples:Dictionary = SampleAnalyzer.GetInstance().GetRawSamplesDict();
hook(samples);
}
SampleAnalyzer.GetInstance().ClearSamples();
SampleAnalyzer.GetInstance().ResumeSampling();
}
public function addWatch(object:Object):WatchHandle {
var handle:WatchHandle;
updateSampling(function(samples:Dictionary):void {
for each(var sample:Sample in samples) {
var newSample:NewObjectSample;
if((newSample = sample as NewObjectSample) != null) {
if(newSample.object == object) {
handle = new WatchHandle(newSample);
_watched.push(handle);
}
}
}
});
return handle;
}
public function isActive(handle:WatchHandle):Boolean {
var ret:Boolean;
updateSampling(function(samples:Dictionary):void{
for each(var sample:Sample in samples) {
var newSample:NewObjectSample;
if((newSample = sample as NewObjectSample) != null) {
if(newSample.id == handle.id) {
ret = true;
break;
}
}
}
});
return ret;
}
public function getActiveWatchedObjects():Array {
var list:Array = [];
updateSampling(function(samples:Dictionary):void {
for each(var handle:WatchHandle in _watched) {
if(samples[handle.id]) {
list.push(handle);
}
}
});
return list;
}
public function getWatchedObjects():Array {
var list:Array = [];
for each(var handle:WatchHandle in _watched) {
list.push(handle);
}
return list;
}
}
class WatchHandle {
private var _id:int;
private var _objectProxy:Dictionary;
private var _type:Class;
private var _stack:Array;
public function get id():int {
return _id;
}
public function get object():Object {
for(var k:Object in _objectProxy) {
return k;
}
return null;
}
public function get stack():Array {
return _stack;
}
public function getFormattedStack():String {
return "\t" + _stack.join("\n\t");
}
public function WatchHandle(sample:NewObjectSample) {
_id = sample.id;
_objectProxy = new Dictionary(true);
_objectProxy[sample.object] = true;
_type = sample.type;
_stack = sample.stack;
}
public function toString():String {
return "[WatchHandle id: " + _id + ", type: " + _type + ", object: " + object + "]";
}
}
And here's a simple demo of how you'd use it.
It initializes the runner, allocates 2 Foo objects and then, after 2 seconds, it finalizes itself. Note that in the finalizer, I'm nulling out one of the Foo objects and finalizing the profiler. There I try to force GC, wait for some time (GC is not synchronous) and then check if these objects are alive. The first object should return false, and the second true. So, this is the place were you'd put your assert. Keep in mind that all of this will only work in a debug player.
So, without any further addo, here's the sample code:
package {
import flash.display.Sprite;
import flash.sampler.NewObjectSample;
import flash.sampler.Sample;
import flash.system.System;
import flash.utils.Dictionary;
import flash.utils.setTimeout;
public class test extends Sprite
{
private var x1:Foo;
private var x2:Foo;
private var _profiler:ProfilerRunner;
private var _watch_x1:WatchHandle;
private var _watch_x2:WatchHandle;
public function test()
{
init();
createObjects();
setTimeout(finalize,2000);
}
public function init():void {
initProfiler();
}
public function finalize():void {
x1 = null;
finalizeProfiler();
}
private function initProfiler():void {
_profiler = new ProfilerRunner();
_profiler.init();
}
private function finalizeProfiler():void {
// sometimes, calling System.gc() in one frame doesn't work
// you have to call it repeatedly. This is a kind of lame workaround
// this should probably be hidden in the profiler runner
var count:int = 0;
var id:int = setInterval(function():void {
System.gc();
count++;
if(count >= 3) {
clearInterval(id);
destroyProfiler();
}
},100);
}
private function destroyProfiler():void {
// boolean check through saved handles
trace(_profiler.isActive(_watch_x1));
trace(_profiler.isActive(_watch_x2));
// print all objects being watched
trace(_profiler.getWatchedObjects());
// get a list of the active objects and print them, plus the alloc stack trace
var activeObjs:Array = _profiler.getActiveWatchedObjects();
for each(var handle:WatchHandle in activeObjs) {
trace(handle);
trace(handle.getFormattedStack());
}
_profiler.destroy();
}
private function createObjects():void {
x1 = new Foo();
x2 = new Foo();
// add them for watch. Also, let's keep a "handle" to
// them so we can query the profiler to know if the object
// is alive or not at any given time
_watch_x1 = _profiler.addWatch(x1);
_watch_x2 = _profiler.addWatch(x2);
}
}
}
import flash.display.Sprite;
class Foo {
public var someProp:Sprite;
}
Alternatively, a more light-weight approach for tracking alive objects is storing them in a weak-referenced dictionary, forcing GC and then checking how many objects are stil alive. Check out this answer to see how this could be implemented. The main difference is that this gives you less control, but maybe it's good enough for your purposes. Anyway, I felt like giving the other idea a shot, so I wrote this object watcher and kind of like the idea.
Since you essentially want weak references, perhaps the best solution would involve one of the weak references available in AS3.
For example, have your method store Dictionaries rather than the actual objects. Something like this:
private var allFoos:Dictionary;
public function getFoo(key:String):Foo {
var f:Foo = _getFoo(key);
if (f == null) {
f = _createFoo(key);
}
return f;
}
private function _createFoo(key:String):Foo {
var f:Foo = new Foo();
var d:Dictionary = new Dictionary(/* use weak keys */ true);
d[f] = key;
allFoos[key] = d;
}
With some intense thinking over the weekend, I believe I figured out what the problem is.
Essentially, we have this scenario:
.--------------.
| APP-DOMAIN 1 |
| [FooFactory] |
'--------------'
|
| < [object Foo]
|
.--------------.
| APP-DOMAIN 2 |
| [MyModule] |
'--------------'
APP-DOMAIN 1 always stays in memory, since it's loaded in the highest app-domain possible: the original compiled code of a SWF. APP-DOMAIN 2 is loaded into and out of memory dynamically and must be able to completely sever itself from APP-DOMAIN 1. According to the genius answer above by Juan Pablo Califano, APP-DOMAIN 2 having a reference to [object Foo] doesn't necessarily tie APP-DOMAIN 2 into memory, though it could become tied into memory by [MyModule] adding an event listener to [object Foo], right?
Okay, so, with this in mind, an overkill solution would be to return a weak-reference-implementation of Foo from the getFoo method, since that's where things need to "break off" in case of "emergency." (Things need to be weak from this perspective so that APP-DOMAIN 1 can be garbage collected completely as it is unloaded.) Again, this is an overkill answer.
However, I do not need to keep a weak-ref to Foo in FooFactory, since FooFactory needs to have a surefire way of getting a hold of each created Foo object. In short, Juan Pablo Califano has the theory completely right, it just needs to be tested in the real world in order to prove everything definitively :)
All of this aside, I believe I have uncovered the real issue behind the scenes that caused a similar library I wrote in the past to never GC. The problem was not in the actual library I wrote, but it seems that it was in a reflection library I was using. The reflection library would "cache" every Class object I threw at it, since my original FooFactory.getFoo method took a Class parameter, rather than a String. Since the library seemed to be hard-referencing every Class object passed into memory, I'm pretty sure that was the memory leak.
In conclusion, as one last question, is it true for me to say that no AS3 solution exists for counting references? I'm building a complete unit-testing suite for this library I'm building, and if I could do something like Assert.assertEquals(0, getReferenceCount(foo)), that would be rad.

ASP.NET/Static class Race Condition?

I have an ASP.NET application with a lot of dynamic content. The content is the same for all users belonging to a particular client. To reduce the number of database hits required per request, I decided to cache client-level data. I created a static class ("ClientCache") to hold the data.
The most-often used method of the class is by far "GetClientData", which brings back a ClientData object containing all stored data for a particular client. ClientData is loaded lazily, though: if the requested client data is already cached, the caller gets the cached data; otherwise, the data is fetched, added to the cache and then returned to the caller.
Eventually I started getting intermittent crashes in the the GetClientData method on the line where the ClientData object is added to the cache. Here's the method body:
public static ClientData GetClientData(Guid fk_client)
{
if (_clients == null)
_clients = new Dictionary<Guid, ClientData>();
ClientData client;
if (_clients.ContainsKey(fk_client))
{
client = _clients[fk_client];
}
else
{
client = new ClientData(fk_client);
_clients.Add(fk_client, client);
}
return client;
}
The exception text is always something like "An object with the same key already exists."
Of course, I tried to write the code so that it just wasn't possible to add a client to the cache if it already existed.
At this point, I'm suspecting that I've got a race condition and the method is being executed twice concurrently, which could explain how the code would crash. What I'm confused about, though, is how the method could be executed twice concurrently at all. As far as I know, any ASP.NET application only ever fields one request at a time (that's why we can use HttpContext.Current).
So, is this bug likely a race condition that will require putting locks in critical sections? Or am I missing a more obvious bug?
If an ASP.NET application only handles one request at a time all ASP.NET sites would be in serious trouble. ASP.NET can process dozens at a time (typically 25 per CPU core).
You should use ASP.NET Cache instead of using your own dictionary to store your object. Operations on the cache are thread-safe.
Note you need to be sure that read operation on the object you store in the cache are threadsafe, unfortunately most .NET class simply state the instance members aren't thread-safe without trying to point any that may be.
Edit:
A comment to this answer states:-
Only atomic operations on the cache are thread safe. If you do something like check
if a key exists and then add it, that is NOT thread safe and can cause the item to
overwritten.
Its worth pointing out that if we feel we need to make such an operation atomic then the cache is probably not the right place for the resource.
I have quite a bit of code that does exactly as the comment describes. However the resource being stored will be the same in both places. Hence if an existing item on rare occasions gets overwritten the only the cost is that one thread unnecessarily generated a resource. The cost of this rare event is much less than the cost of trying to make the operation atomic every time an attempt to access it is made.
This is very easy to fix:
private _clientsLock = new Object();
public static ClientData GetClientData(Guid fk_client)
{
if (_clients == null)
lock (_clientsLock)
// Check again because another thread could have created a new
// dictionary in-between the lock and this check
if (_clients == null)
_clients = new Dictionary<Guid, ClientData>();
if (_clients.ContainsKey(fk_client))
// Don't need a lock here UNLESS there are also deletes. If there are
// deletes, then a lock like the one below (in the else) is necessary
return _clients[fk_client];
else
{
ClientData client = new ClientData(fk_client);
lock (_clientsLock)
// Again, check again because another thread could have added this
// this ClientData between the last ContainsKey check and this add
if (!clients.ContainsKey(fk_client))
_clients.Add(fk_client, client);
return client;
}
}
Keep in mind that whenever you mess with static classes, you have the potential for thread synchronization problems. If there's a static class-level list of some kind (in this case, _clients, the Dictionary object), there's DEFINITELY going to be thread synchronization issues to deal with.
Your code really does assume only one thread is in the function at a time.
This just simply won't be true in ASP.NET
If you insist on doing it this way, use a static semaphore to lock the area around this class.
you need thread safe & minimize lock.
see Double-checked locking (http://en.wikipedia.org/wiki/Double-checked_locking)
write simply with TryGetValue.
public static object lockClientsSingleton = new object();
public static ClientData GetClientData(Guid fk_client)
{
if (_clients == null) {
lock( lockClientsSingleton ) {
if( _clients==null ) {
_clients = new Dictionary``();
}
}
}
ClientData client;
if( !_clients.TryGetValue( fk_client, out client ) )
{
lock(_clients)
{
if( !_clients.TryGetValue( fk_client, out client ) )
{
client = new ClientData(fk_client)
_clients.Add( fk_client, client );
}
}
}
return client;
}

Resources