SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Apr 2009
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Indifinite loop - How to end?

    please excuse me, i'm not a developer but a tester and i'm no good at doing programming stuff and all. im into automation now for my testing and i choose to use ruby for it but i have a problem hope you guys could help me out of this...

    i need to do a script that will check my email every 5minutes and after 20minutes of no new mail it will produce "test fail". i have found a cool code for checking email every 5min but i could not find a way to stop it after 20minutes? any help is greatly appreciated

    here's the code:

    def time_block
    start_time = Time.now
    Thread.new { yield }
    Time.now - start_time
    end

    def repeat_every(seconds)
    begin
    time_spent = time_block { yield }
    sleep(seconds - time_spent)
    end while time_spent < seconds
    end

    repeat_every(300) { # checking of email ever 5 minutes
    puts "Start checking: #{Time.now}"
    # email checking here
    puts "End checking: #{Time.now}"
    }

  2. #2
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:
    Code:
    def time_block
      start_time = Time.now
      Thread.new { yield }
      Time.now - start_time
    end
    
    def repeat_for(delay_in_seconds, number_of_loops)
      loop = 0
      begin
      time_spent = time_block { yield }
      sleep(delay_in_seconds - time_spent)
      loop += 1
      end while loop < number_of_loops
    end
    
    repeat_for(300, 4) { # checking of email ever 5 minutes, and 4 times
      puts "Start checking: #{Time.now}"
      # email checking here
      puts "End checking: #{Time.now}"
    }

  3. #3
    SitePoint Member
    Join Date
    Apr 2009
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you so much... it does works...Godspeed!!!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •