Sometimes a small bug can lead to headaches after death.

Yesterday I wrote a plugin for RT which allows one to use the members in a group as the values for a custom field. This is useful because it allows for user-signoff functionality. The problem to tackle was specifying which group to use. The obvious answer was to use an attribute on the custom field. The custom field was passed into the plugin via an inherited method. I chose to override this method and capture the field so that I could grab its attribute.

The solution would have worked except that I assumed the method was not needed anywhere else, a very silly mistake in hindsight. So I fired up RT and sure enough it did not work as expected. Since my mistake was very silly indeed, it was easy to spot and correct. Following good practices, however, I decided to add an additional test that covered what I had missed previously.

If I had fired up RT as soon as I "fixed" the problem, I would have seen it working as desired and avoided a huge headache. Unfortunately, however, I did not fire up RT; I ran my tests instead. To my dismay my new tests were failing. I was sure I had done everything right, so I double checked:

* Field Created - Check
* Attribute added - Check
* Attribute exists - Check
* Using my class worked - Check
* Asking the field for its values - Fail

At this point I began to suspect a bug in RT, or incorrect documentation, because I was sure I had done everything correctly. I was right, too. It was all correct. The problem was that when I first started working on RT I had to add and retrieve attributes manually using the RT::Attribute class. However RT has an RT::Record class that most RT classes inherit from. Within this class are some methods for adding and retrieving Attributes on an object. Unlike me, RT used these methods.

I am not sure if my inexperience 6 months ago led me to miss them, or if they simply were not in RT at that version (we previously used 3.6, we now use 3.8) but for whatever reason when I first looked for them I did not find them, and have been doing it the hard way ever since. After a very long time of poking at every little thing trying to find my mistake, or an RT bug, I eventually found these methods.

I Added a test to retrieve the attribute using the RT method... no luck, test failed. So I looked more deeply at the methods. What I found made me slap my forehead. RT caches attributes when an object is created/loaded. As such I would crate my custom field and then my attribute, however since I did not use the method, the cache was not updated. Because of this very few components, if any, would be able to find it.

This cache issue is not one that ever would have come up during usage. Our system loads data such as attributes into the database well before the server is started, because of that they would be present. This is why firing up the app would have shown that it was indeed working. I was able to update my test, and now everything passes.

Lessons:
* When you think 'hey, that method would be useful, but there is none' take note to check every version update to see if it was added.
* If you know your application does caching, and you are not getting data you KNOW you gave it, make sure the cache is updated.

Tagged as: RT