<commit_before>// Copyright (c) 2010 - 2013 Leap Motion. All rights reserved. Proprietary and confidential.
#include "stdafx.h"
#include "DispatchQueueTest.h"
#include "DispatchQueue.h"

using namespace std;

class EventMaker:
  public CoreThread
{
public:
  EventMaker():
    CoreThread("EventMaker")
  {
    AcceptDispatchDelivery();
  }
};


class ThreadBase:
  public CoreThread
{
public:
  ThreadBase(){
    AcceptDispatchDelivery();
  }
};

template<int N>
class Thread:
  public ThreadBase
{};


TEST_F(DispatchQueueTest, SimpleEvents) {
  int count = 0;
  
  *this += [&count] () {
    ++count;
  };
  
  *this += [&count] () {
    count += 5 ;
  };
  
  int num = DispatchAllEvents();
  
  *this += [&count] () {
    count += 5 ;
  };
  
  EXPECT_EQ(2, num);
  EXPECT_EQ(6, count);
}

TEST_F(DispatchQueueTest, SingleThreadCommits){
  int count = 0;
  {
    Commision com = *this += [&count] () {
      count+=1;
    };
    
    DispatchAllEvents();
  
    *this += [&count] () {
      count+=2;
    };
    
    DispatchAllEvents();
  
    Commision com2 = *this += [&count] {
      count+=4;
    };
    
    DispatchAllEvents();
    
    EXPECT_EQ(0, count);
    
    com.Commit();
    
    DispatchAllEvents();
    
    EXPECT_EQ(3, count);
  }
  
  DispatchAllEvents();
  
  EXPECT_EQ(7, count);
}

#if _WIN32
TEST_F(DispatchQueueTest, DISABLED_MultiThreadCommits)
#else
TEST_F(DispatchQueueTest, MultiThreadCommits)
#endif
{
  AutoRequired<EventMaker> eventMaker;
  int count = 0;
  
  boost::mutex mut;
  boost::unique_lock<boost::mutex> lk(mut);
  boost::condition_variable cond;
  
  m_create->InitiateCoreThreads();
  
  *eventMaker += [&count, &cond, &mut] () {
    boost::unique_lock<boost::mutex> lk(mut);
    count += 1;
    cond.notify_all();
  };
  
  Commision com1 = *eventMaker += [&count, &cond, &mut] () {
    boost::unique_lock<boost::mutex> lk(mut);
    count += 2;
  };
  
  *eventMaker += [&count, &cond, &mut] () {
    boost::unique_lock<boost::mutex> lk(mut);
    count += 4;
    cond.notify_all();
  };
  
  EXPECT_FALSE( boost::cv_status::timeout == cond.wait_for(lk,boost::chrono::microseconds(500)) )
  << "Waited more than 500ms on condition variable";
  
  EXPECT_EQ(1, count);
  
  com1.Commit();
  
  EXPECT_FALSE( boost::cv_status::timeout == cond.wait_for(lk,boost::chrono::microseconds(500)) )
  << "Waited more than 500ms on condition variable";
  <commit_msg>Lengthened wait time in DispathQueueTest.<commit_after>// Copyright (c) 2010 - 2013 Leap Motion. All rights reserved. Proprietary and confidential.
#include "stdafx.h"
#include "DispatchQueueTest.h"
#include "DispatchQueue.h"

using namespace std;

class EventMaker:
  public CoreThread
{
public:
  EventMaker():
    CoreThread("EventMaker")
  {
    AcceptDispatchDelivery();
  }
};


class ThreadBase:
  public CoreThread
{
public:
  ThreadBase(){
    AcceptDispatchDelivery();
  }
};

template<int N>
class Thread:
  public ThreadBase
{};


TEST_F(DispatchQueueTest, SimpleEvents) {
  int count = 0;
  
  *this += [&count] () {
    ++count;
  };
  
  *this += [&count] () {
    count += 5 ;
  };
  
  int num = DispatchAllEvents();
  
  *this += [&count] () {
    count += 5 ;
  };
  
  EXPECT_EQ(2, num);
  EXPECT_EQ(6, count);
}

TEST_F(DispatchQueueTest, SingleThreadCommits){
  int count = 0;
  {
    Commision com = *this += [&count] () {
      count+=1;
    };
    
    DispatchAllEvents();
  
    *this += [&count] () {
      count+=2;
    };
    
    DispatchAllEvents();
  
    Commision com2 = *this += [&count] {
      count+=4;
    };
    
    DispatchAllEvents();
    
    EXPECT_EQ(0, count);
    
    com.Commit();
    
    DispatchAllEvents();
    
    EXPECT_EQ(3, count);
  }
  
  DispatchAllEvents();
  
  EXPECT_EQ(7, count);
}

TEST_F(DispatchQueueTest, MultiThreadCommits) {
  AutoRequired<EventMaker> eventMaker;
  int count = 0;
  
  boost::mutex mut;
  boost::unique_lock<boost::mutex> lk(mut);
  boost::condition_variable cond;
  
  m_create->InitiateCoreThreads();
  
  *eventMaker += [&count, &cond, &mut] () {
    boost::unique_lock<boost::mutex> lk(mut);
    count += 1;
    cond.notify_all();
  };
  
  Commision com1 = *eventMaker += [&count, &cond, &mut] () {
    boost::unique_lock<boost::mutex> lk(mut);
    count += 2;
  };
  
  *eventMaker += [&count, &cond, &mut] () {
    boost::unique_lock<boost::mutex> lk(mut);
    count += 4;
    cond.notify_all();
  };
  
  ASSERT_EQ(boost::cv_status::no_timeout, cond.wait_for(lk,boost::chrono::seconds(2)))
  << "Waited more than 2 seconds on condition variable. Probably hanging.";
  
  EXPECT_EQ(1, count);
  
  com1.Commit();
  
  ASSERT_EQ(boost::cv_status::no_timeout, cond.wait_for(lk,boost::chrono::seconds(2)))
  << "Waited more than 2 seconds on condition variable. Probably hanging.";
  
{'fraction_non_alphanumeric': 0.15862068965517243, 'fraction_numerical': 0.015464994775339603, 'mean_word_length': 3.25044404973357, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1418443', 'n_tokens_mistral': 1795, 'n_tokens_neox': 1743, 'n_words': 336}