Le-Blog-à-Dam

Aller au contenu | Aller au menu | Aller à la recherche

mardi 17 octobre 2006

Version 0.2 of the Cache Test plugin

As the title implies, I have released a new version of my cache logic test plugin.

Ok, I must admit I have been a bit motivated by that post =P, but anyway, here you are with some new features.

First you are now able to check the actions and fragments caching logic: there are four new methods:

- assert\_cache\_actions - assert\_expire\_actions - assert\_cache\_fragments - assert\_expire\_fragments

They work quite like the corresponding page method, see the documentation for code and examples.

I also changed the way page caching test works. The API is still the same, just the internals have changed. Instead of checking that the page’s cache files are created or removed, I now hijack the ActionController::Base methods so that they records which page are cached or expired. I then use this information in the assertions. No more fiddling with the file system to know if a file is cached or not.

And finally, this release comes with some unit test cases.

The fragment code seems to be working, but I have no application that makes heavy use of fragments so if you find bug in it or have suggestions, don’t hesitate to tell me!

update: I think I have a better idea for the API something along the line of

1
2
3
4
5
6
7
8
9
10
11
12
13
14

def test_caching
  assert_caching do
    get "/my/page"
    assert_page_cached "/my/page"

    get "/my/action"
    assert_action_cached :controller => "my", :action => "action"

    get "/my/fragment"
    assert_fragment_cached :controller => "my", 
                                            :action => "fragment",
                                            :fragment => "foo"
  end
end

I’ve already implemented it, I’ll wait for some feedback before releasing a new version.

jeudi 5 octobre 2006

Testing ruby on rails page caching

For the new version of iSearch, I used the page caching feature of Ruby on Rails a lot. I also needed to refresh those pages when some model where added, updated or deleted, so I needed to use cache sweeping too.

Now caching is great, but you need to be sure it works, and what's the best thing you can do to make sure it works? Write tests! But here we have a problem, as I could not find any information about page caching testing. So I decided to add some page caching test helpers to my application, and while I'm at it, released them as a plugin.

Here, I will explain how the plugin is working. Go to the plugin page if you want more information about how to download, install and use it.

First let's have a look at the init.rb file:

1
2
3
4
5
6
7

if RAILS_ENV == "test"
  require 'page_cache_test'

  Test::Unit::TestCase.class_eval do
    include Cosinux::PageCacheTest
  end
end

On line 1, we make sure that we are in the test environment so we don't interfere with other environments. If it is the case, we first load the page_cache_test.rb file on line 2. Then we include the module Cosinux::PageCacheTest in the Test::Unit::TestCase on line 4 to 6.

Now let's go in the page_cache_test.rb and see what happens when the Cosinux::PageCacheTest module is included.

1
2
3
4
5
6

module Cosinux::PageCacheTest
  def self.included(base)
    ActionController::Base.public_class_method :page_cache_path
    ActionController::Base.perform_caching = true
  end
end

The method self.included on line two, defined in the Module class is executed each time a module is included. So here it is executed because of the include we've just seen in the init.rb file.

In this method, on line 3, we change the visibility of the method page_cache_path of ActionController::Base so that we can use it in our assertions. The method is used to get the path of the cached file for a url.

Then, on line 4, we ensure that caching is enabled (it is normally disabled in the test environment).

So were are we now ? We included the Cosinux::PageCacheTest module in the Test::Units::TestCase class so each method defined in the module is available in the class. There are two instance methods defined in the module:

  • assert_cache,
  • and assert_expire.

Let's have a look at both of them.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

def assert_cache(*urls)
  silence do
    urls.each do |url|
      ActionController::Base.expire_page(url)
    end
  end

  if block_given?
    yield *urls
  else
    urls.each { |url| get url }
  end

  urls.each do |url|
    assert_block("#{url.inspect} is not " +
                         "cached after executing block") do
      File.exists? page_cache_path(url)
    end
  end
end

First, on line 3 to 5, we ensure that the urls we are checking are not in cache by expiring them on line 4. We also make sure that no message is logged by using the silence method which is defined at the end of the file.

Then, on line 8 and 9, if a block is given, we execute it, giving it the urls as arguments. Otherwise, on line 11, we execute a get request on each of the given urls.

Finaly, on line 14 to 18, we make sure that the urls are cached by checking that the corresponding files exists on the file system.

Now to the other method.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

def assert_expire(*urls)
  silence do
    urls.each do |url|
      ActionController::Base.cache_page("testing", url)
    end
  end

  yield *urls

  urls.each do |url|
    assert_block("#{url.inspect} is cached" +
                         " after executing block") do
      ! File.exists? page_cache_path(url)
    end
  end
end

As you can see, it looks quite the same, excepts a few things.

First it makes sure that the urls are cached by writing "testing" in the corresponding files on line 4.

Then we always execute a block, so that the user can take some actions that should make the urls expire, like triggering an action which defines a sweeper.

And finally, on line 12, we assert that the files corresponding to the urls do not exists anymore.

That's it. I hope this will explain how this small plugin work and that it will help somebody. Next time, I will write about my experiments with a plugin to create multiple page forms.

mardi 3 octobre 2006

Un petit frère ou une petite soeur ?

Ca fait de nouveau longtemps que je n'ai plus donné de nouvelles. En fait, vu que je rentre tout les jours vers 19h30, je n'ai pas trop le temps de blogger.

Pour ceux qui ne sont pas encore au courant (si il y en a encore), Chrystelle attend famille et nous espérons avoir un petit frère ou une petite soeur pour Suzanne aux environs de début mai. Jusqu'ici, tout va bien, Chrystelle est juste un peu fatiguée.

Le week-end passé, nous sommes allés à Walibi avec la famille: papa, maman, ma soeur, mon frère, Sophie, Chrystelle, Suzanne, Julien, ma filleule et moi. La journée a été excellente, et je n'ai même pas été malade bien que Julien nous ait obligé à aller sur le Dalton Terror, un bazar qui tombe de très haut très vite et qui vous met le coeur dans les cheveux.

Ca c'est pour dimanche. Samedi, c'était notre dernier passage en tant qu'assistants d'unité au scout à Fexhe. Après presque 10 ans dans l'unité, je lève le pied. Toutes ces années ont été terribles (dans le bon sens) et je remercie tout les gens que j'ai croisé grâce aux scouts. Et bonne m**** pour la suite à l'unité Zénon Sacré!

Le week-end encore avant, je suis allé sauter en parachutte avec mon frère. Expérience réussie, sensation extraordinaire. Je vais voir ce que je peux faire pour mettre la video sur le web. En attendant, mon frère a quelques photos. Il ne reste plus qu'à trouver 1200€ pour passer le brevet ;)

En parlant de photo, il m'en reste un paquet à mettre sur le web, je vais essayer de faire ça un de ses quatres.

En vous souhaitant bien le bonsoir,

new iSearch version

I have updated my index search engine, you can see (and use) the results at http://isearch.cosinux.org/.

What's new ?

  • it uses Ferret to search the links, so you can use ferret search syntax, and search is quicker,
  • it uses Hpricot to extract the links from the index so you can extract only the needed links,
  • and I hope it has a better look.

I have created to plugin for this project that I will release one I find the time:

  • a plugin to creates multiple page form,
  • and a plugin to test page caching.

I'll try to blog about that soon.

mercredi 27 septembre 2006

Microformats

Have you heard of microformats ?

Microformats are usings predefined XHTML snippets to show common elements in web pages. For example to display a contact, you could use the hCard which allow you to represent a vCard (like those used in most email tools).

Why do that ? The hope is that in the near future, you will be able for example to drag and drop a contact on a web page directly into your contact manager application.

I think I will try to follow those recommendation, so that when the day has come, I will be microformats ready!

UPDATE: the day has already come, (technorati)[http://technorati.com/contacts/] has a small web application that converts and hCard to a vCard and allows your browser to download it and add it to your contact manager.

- page 4 de 9 -