<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title><![CDATA[Dexta Blogs]]></title>
  <link href="http://www.dextablogs.com/atom.xml" rel="self"/>
  <link href="http://www.dextablogs.com/"/>
  <updated>2014-01-07T09:55:29+05:30</updated>
  <id>http://www.dextablogs.com/</id>
  <author>
    <name><![CDATA[RC]]></name>
    
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[Running cucumber features in different locale]]></title>
    <link href="http://www.dextablogs.com/blog/2013/12/25/running-cucumber-features-in-different-locale/"/>
    <updated>2013-12-25T10:59:30+05:30</updated>
    <id>http://www.dextablogs.com/blog/2013/12/25/running-cucumber-features-in-different-locale</id>
    <content type="html"><![CDATA[<p>We use Selenium + Capybara + Firefox to run all our cucumber features. Recently, we decided to extend the tests to run in different locales and this post is a summary of the problems and the plausible solutions.</p>

<p>We had the following objectives in mind:</p>

<ul>
<li>Since, we run the tests in firefox, the test process should be capable of creating an appropriate profile with required locale settings</li>
<li>The tests should be generic enough so that they don&rsquo;t have to be written and maintained separately for every locale</li>
</ul>


<h2>Setup</h2>

<p>The first step involves creating a new browser profile and switching the locale (italian in this case) and passing it on to the Capybara driver.</p>

<figure class='code'><figcaption><span>env.rb</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="no">Capybara</span><span class="o">.</span><span class="n">default_driver</span> <span class="o">=</span> <span class="ss">:selenium</span>
</span><span class='line'>
</span><span class='line'><span class="n">it_profile</span> <span class="o">=</span> <span class="ss">Selenium</span><span class="p">:</span><span class="ss">:WebDriver</span><span class="o">::</span><span class="ss">Firefox</span><span class="p">:</span><span class="ss">:Profile</span><span class="o">.</span><span class="n">new</span>
</span><span class='line'><span class="n">it_profile</span><span class="o">[</span><span class="s1">&#39;intl.accept_languages&#39;</span><span class="o">]</span> <span class="o">=</span> <span class="s1">&#39;it&#39;</span> <span class="c1"># Italian Locale</span>
</span><span class='line'>
</span><span class='line'><span class="no">Capybara</span><span class="o">.</span><span class="n">register_driver</span> <span class="ss">:selenium</span> <span class="k">do</span> <span class="o">|</span><span class="n">app</span><span class="o">|</span>
</span><span class='line'>  <span class="ss">Capybara</span><span class="p">:</span><span class="ss">:Selenium</span><span class="o">::</span><span class="no">Driver</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="n">app</span><span class="p">,</span> <span class="ss">:browser</span> <span class="o">=&gt;</span> <span class="ss">:firefox</span><span class="p">,</span>
</span><span class='line'>                                      <span class="ss">:profile</span> <span class="o">=&gt;</span> <span class="n">it_profile</span><span class="p">)</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="no">Capybara</span><span class="o">.</span><span class="n">current_session</span><span class="o">.</span><span class="n">driver</span><span class="o">.</span><span class="n">browser</span><span class="o">.</span><span class="n">capabilities</span><span class="o">.</span><span class="n">firefox_profile</span> <span class="o">=</span> <span class="n">it_profile</span>
</span></code></pre></td></tr></table></div></figure>


<p>The advantage of creating a profile on-the-fly is that the tests do not require every machine to have a pre-configured firefox profile that has the necessary settings.</p>

<!-- more -->


<h2>Using translation keys in step definitions</h2>

<p>Inside step definition, instead of hard-coding text, use the translation keys directly. This is a good practice even when writing tests for a single locale as your tests are not affected by textual changes.</p>

<figure class='code'><figcaption><span>alert_steps.rb</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="no">When</span> <span class="sr">/^I click on the stock alert button$/</span> <span class="k">do</span> <span class="o">|</span><span class="n">arg</span><span class="o">|</span>
</span><span class='line'>  <span class="n">find</span><span class="p">(</span><span class="n">button</span><span class="p">,</span> <span class="ss">text</span><span class="p">:</span> <span class="n">t</span><span class="p">(</span><span class="s1">&#39;stock_tab.alert_caption&#39;</span><span class="p">))</span><span class="o">.</span><span class="n">click</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>When it&rsquo;s a step with text as parameter, use the key directly in the feature. Yes, this is not so readable as using the actual text but using sensible translation keys will take it one step closer to using actual text.</p>

<figure class='code'><figcaption><span>admin.feature </span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='cucumber'><span class='line'><span class="k">Given </span><span class="nf">I am logged in as administrator</span>
</span><span class='line'><span class="k">And </span><span class="nf">I click on t(settings)</span>
</span></code></pre></td></tr></table></div></figure>


<p>The key passed as parameter can be used with I18n directly.</p>

<figure class='code'><figcaption><span>admin_steps.rb</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="no">And</span> <span class="sr">/I click on t\(:?([^\)]*)\)/</span> <span class="k">do</span> <span class="o">|</span><span class="n">key</span><span class="o">|</span>
</span><span class='line'>  <span class="n">find</span><span class="p">(</span><span class="n">button</span><span class="p">,</span> <span class="ss">text</span><span class="p">:</span> <span class="n">t</span><span class="p">(</span><span class="n">key</span><span class="p">))</span><span class="o">.</span><span class="n">click</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[What's the deal with Ruby GC and Copy-on-write]]></title>
    <link href="http://www.dextablogs.com/blog/2013/01/02/What-the-deal-with-ruby-garbage-collection-and-copy-on-write/"/>
    <updated>2013-01-02T21:06:00+05:30</updated>
    <id>http://www.dextablogs.com/blog/2013/01/02/What-the-deal-with-ruby-garbage-collection-and-copy-on-write</id>
    <content type="html"><![CDATA[<p>This post aims at answering the following questions:</p>

<ul>
<li>What is Unix Copy-on-write (COW)</li>
<li>Why is current version of ruby (1.x.x) not COW friendly</li>
<li>How does the GC packaged with Ruby 2.0 fix that</li>
</ul>


<h2>Holy COW</h2>

<p>The fork functionality in Unix systems uses an optimization strategy where memory is shared between the parent and the child processes. The shared memory is maintained till either the parent or one of the children modify their copy of the resource. At that point, a true private copy is created to prevent the changes from being visible to other processes. The primary advantage is that if any of the processes do not make any modifications, no private copy needs to be ever created. This is called Copy-on-write (COW) technique.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">shared_array</span> <span class="o">=</span> <span class="o">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="o">]</span>
</span><span class='line'>
</span><span class='line'><span class="k">if</span> <span class="nb">fork</span>
</span><span class='line'>  <span class="c1">#will be executed by parent process</span>
</span><span class='line'>  <span class="n">parent_array</span> <span class="o">=</span> <span class="o">[</span><span class="mi">6</span><span class="p">,</span><span class="mi">7</span><span class="p">,</span><span class="mi">8</span><span class="o">]</span>
</span><span class='line'><span class="k">else</span>
</span><span class='line'>  <span class="c1">#will be executed by child process</span>
</span><span class='line'>  <span class="n">child_array</span> <span class="o">=</span> <span class="o">[</span><span class="mi">9</span><span class="p">,</span><span class="mi">10</span><span class="p">,</span><span class="mi">11</span><span class="o">]</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>




<!-- more -->


<p>If you are not familiar with Ruby fork, a call to <code>fork</code> creates a new process. In the above example, the code inside the if block will be executed by the parent process and else block will be executed by the child process (In the child process fork returns nil). A snapshot of how resources will be shared in memory is shown below.</p>

<p><img class="left" src="http://www.dextablogs.com/images/2013-01-02/shared_pool.png"></p>

<p>As can be seen above, the <em>shared_array</em> is maintained as a common resource between parent and child processes. When <em>shared_array</em> is modified by the child process, as copy-on-write implies, a private copy is created so that it is not visible to the other process. This is illustrated below.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">shared_array</span> <span class="o">=</span> <span class="o">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="o">]</span>
</span><span class='line'>
</span><span class='line'><span class="k">if</span> <span class="nb">fork</span>
</span><span class='line'>  <span class="c1">#will be executed by parent process</span>
</span><span class='line'>  <span class="n">parent_array</span> <span class="o">=</span> <span class="o">[</span><span class="mi">6</span><span class="p">,</span><span class="mi">7</span><span class="p">,</span><span class="mi">8</span><span class="o">]</span>
</span><span class='line'><span class="k">else</span>
</span><span class='line'>  <span class="c1">#will be executed by child process</span>
</span><span class='line'>  <span class="n">shared_array</span> <span class="o">&lt;&lt;</span> <span class="mi">5</span>
</span><span class='line'>  <span class="n">child_array</span> <span class="o">=</span> <span class="o">[</span><span class="mi">9</span><span class="p">,</span><span class="mi">10</span><span class="p">,</span><span class="mi">11</span><span class="o">]</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p><img class="left" src="http://www.dextablogs.com/images/2013-01-02/private_copy.png"></p>

<h2>Mark and Sweep objects</h2>

<p>The advantages of Copy-on-write cannot be leveraged between multiple ruby processes due to the inherent nature of the way the garbage collector works in Ruby. The garbage collector uses a simple <a href="http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29#Na.C3.AFve_mark-and-sweep">Mark and Sweep</a> algorithm to identify unused objects. In simplest terms</p>

<blockquote><p>Each object in memory has a flag (typically a single bit) reserved for the garbage collector. Starting from the root-set, all objects that can be accessed are recursively traversed and marked as being &lsquo;in-use&rsquo;. At the end of the cycle, the GC sweeps all objects that have not been marked and restores free space for future objects.</p></blockquote>

<p>The way ruby creates objects, the GC flag or reserved bit is stored in the object itself. So, as you would have guessed by now, when the GC runs in one of the processes, the GC flag would be modified in all the objects even if they are present in the shared pool. Now, the OS would sense this and trigger a copy-on-write making private copies of the objects in each child&rsquo;s memory space.</p>

<p>This is the reason why Ruby 1.8 or 1.9 is not COW friendly :(</p>

<h2>GC Bit and objects should keep distance</h2>

<p>The most sensible thing to do would be to pull out the GC flag (it&rsquo;s actually called FL_MARK bit) from objects and maintain them separately. And this is exactly what Ruby 2.0 claims to do.</p>

<p><img class="left" src="http://www.dextablogs.com/images/2013-01-02/bitmap.png"></p>

<p>For each heap allocated by ruby, there is a corresponding bitmap which is linked to the header of the heap. The bitmap can store 0 or 1 values effectively replacing the GC bit which was previously stored inside the objects present in the heap. This technique is called bitmap marking. So in effect, the <em>Mark step</em> will not modify any live objects in the heap. Only the bitmap will be changed. Hence, the shared objects can remain that way until one of the processes actually modified them.</p>

<h3>References:</h3>

<ol>
<li><a href="http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/41916">Narihiro Nakamura&rsquo;s patch for Bitmap Marking GC</a></li>
<li><a href="http://patshaughnessy.net/2012/3/23/why-you-should-be-excited-about-garbage-collection-in-ruby-2-0">Pat Shaughnessy&rsquo;s excellent blog post</a></li>
</ol>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[3 ways to keep a Linux process alive]]></title>
    <link href="http://www.dextablogs.com/blog/2012/12/25/3-ways-to-keep-a-linux-process-alive/"/>
    <updated>2012-12-25T10:00:00+05:30</updated>
    <id>http://www.dextablogs.com/blog/2012/12/25/3-ways-to-keep-a-linux-process-alive</id>
    <content type="html"><![CDATA[<p>Assume that you have a ruby script <code>data_cruncher.rb</code> that will run for a long time. You would like to log into a VM, run the script and keep it running in the background when you log out. Now, I end up writing long running scripts like these all the time and I found the following ways to make them run uninterrupted:</p>

<h2>1. Job Control</h2>

<p><a href="http://mywiki.wooledge.org/BashGuide/JobControl">Job control</a> allows you to interact with background jobs, suspend foreground jobs and manage multiple jobs in a single session. Also, when you later realize that the currently running command must be pushed to the background, job control makes it easy to do that.</p>

<ul>
<li>Start running the process using the command <code>ruby data_cruncher.rb</code></li>
<li>Press <kbd>ctrl</kbd>+<kbd>z</kbd> to suspend the process</li>
<li>type <code>bg</code> to resume it in the background</li>
</ul>


<p>That&rsquo;s it. The job will run in the background. Note that using <code>bg</code> command later is the same as running the parent process with &amp; at the end like this: <code>ruby data_cruncher.rb &amp;</code>.</p>

<!-- more -->


<p>You can verify active jobs by using the <code>jobs</code> command. The <code>-l</code> option will include the process ID.</p>

<figure class='code'><figcaption><span>bash.sh </span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='sh'><span class='line'><span class="nv">$ </span><span class="nb">jobs</span> -l
</span><span class='line'>1<span class="o">]</span>  + 13701 running    ruby data_cruncher.rb
</span></code></pre></td></tr></table></div></figure>


<p>Note that the number 1 at the beginning is the jobspec which is a number allocated to each job. 13701 is the process ID. The jobspec is useful when there are multiple active jobs. The significance of jobspec is illustrated below.</p>

<p>$ jobs -l
1]  + 13701 running    ruby data_cruncher.rb
2]  + 14312 running    tail -f data_cruncher.log
$ fg %1
[1]  + 13701 continued  ruby data_cruncher.rb</p>

<p>fg is used to pop out a process to the foreground. fg and bg commands can identify specific jobs with <code>%&lt;jobspec&gt;</code>. If you are using bash shell, before you logout, you will have to fire another command <code>disown -h</code> to make sure the process is not killed when the terminal session ends. <code>-h</code> is used to ignore HUP signal. For more information on HUP, see next section.</p>

<figure class='code'><figcaption><span>bash.sh </span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='sh'><span class='line'><span class="nv">$ </span><span class="nb">jobs</span> -l
</span><span class='line'>1<span class="o">]</span>  + 13701 running    ruby data_cruncher.rb
</span><span class='line'>2<span class="o">]</span>  + 14312 running    tail -f data_cruncher.log
</span><span class='line'><span class="nv">$ </span><span class="nb">disown</span> %1
</span><span class='line'><span class="nv">$ </span><span class="nb">jobs</span> -l
</span><span class='line'>1<span class="o">]</span>  + 14312 running    tail -f data_cruncher.log
</span></code></pre></td></tr></table></div></figure>


<p>What disown actually does is mark the job so that <code>SIGHUP</code> signal is not sent to the job when the parent receives it. To explain SIGHUP, let&rsquo;s dive into the next technique.</p>

<h2>2. nohup to the rescue</h2>

<p>To give some context, inter-process communication happens in linux using signals. When a signal is sent, the OS interrupts the normal flow of execution of the process. The most important signals relevant to this blog post are described below. For an exhaustive list, see <a href="http://en.wikipedia.org/wiki/Unix_signal">here</a></p>

<p><strong>SIGINT</strong> (<kbd>CTRL</kbd>+<kbd>C</kbd>) &ndash; Sent to a process from controlling terminal when user wishes to interrupt the process</p>

<p><strong>SIGTSTP</strong> (<kbd>CTRL</kbd>+<kbd>Z</kbd>) &ndash; Sent to a process from controlling terminal to request it to stop temporarily</p>

<p><strong>SIGHUP</strong> &ndash; Sent to a process when controlling terminal is closed</p>

<p>When a user logs out of a terminal session, the HUP signal is used to warn all dependent processes of the logout action. nohup command is used to ignore the HUP signal for a specific process.</p>

<figure class='code'><figcaption><span>bash.sh </span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='sh'><span class='line'><span class="nv">$ </span>nohup ruby data_cruncher.rb &amp;
</span></code></pre></td></tr></table></div></figure>


<p>This will cause the script to run in the background and will pay no heed to HUP signal, if received. By default, STDOUT and STDERR will be redirected to nohup.out. To explicitly mention the output log file,</p>

<figure class='code'><figcaption><span>bash.sh </span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='sh'><span class='line'><span class="nv">$ </span>nohup ruby data_cruncher.rb &gt; data_cruncher.log 2&gt;&amp;1 &amp;
</span></code></pre></td></tr></table></div></figure>


<p>What does <code>2&gt;&amp;1</code> mean? 1 and 2 are the standard file descriptors for STDOUT and STDERR. We redirect STDERR to STDOUT. Why the ampersand? <code>1</code> instead of <code>&amp;1</code> would indicate a regular file by the name 1 and not the STDOUT file descriptor.</p>

<h2>3. Screen your commands</h2>

<p>Screen is the most sophisticated way to run background jobs when you want to continously  monitor their activity. According to wikipedia,</p>

<blockquote><p>GNU Screen is a software application that can be used to multiplex several virtual consoles, allowing a user to access multiple separate terminal sessions inside a single terminal window or remote terminal session.</p></blockquote>

<p>What follows is a smash-course of Screen:</p>

<ul>
<li>Start screen by typing <code>screen</code>. You will be greeted with a welcome message.</li>
<li>Every program under screen runs in a virtual window. The windows are numbered from 0.</li>
<li>You are currently in window 0. To start another window, press <kbd>ctrl</kbd>+<kbd>A</kbd>+<kbd>C</kbd></li>
<li>Now you have two windows 0 and 1. There are several ways to switch between them. We will use <kbd>ctrl</kbd>+<kbd>A</kbd>+<kbd>&ldquo;</kbd></li>
<li>You should see all the windows that are active. Choose the one you want.</li>
</ul>


<p>Now, this is where things get interesting. Now press <kbd>ctrl</kbd>+<kbd>A</kbd>+<kbd>D</kbd> You will be detached from current screen session and back to the bash shell. <em>Note that the session has not been killed and all the programs that you started running with screen will be still active.</em></p>

<p>To reattach, type <code>screen -r</code> Press <kbd>ctrl</kbd>+<kbd>A</kbd>+<kbd>&ldquo;</kbd> and you see that nothing has been lost. One obvious perk of screen that job control or nohup does not offer is that you always have easy and immediate access to the process that you were running in the background.</p>

<p>Screen is an awesome utility and this <a href="http://kb.iu.edu/data/acuy.html">page</a> has more valuable info.</p>
]]></content>
  </entry>
  
</feed>
