ThreadingTests.cpp 3.45 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/******************************************************************************
 *
 * This file is part of the Castor project.
 * See http://castor.web.cern.ch/castor
 *
 * Copyright (C) 2003  CERN
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * 
 *
 * @author Castor Dev team, castor-dev@cern.ch
 *****************************************************************************/

24
#include "castor/server/MutexLocker.hpp"
25
26
27
#include "castor/server/Threading.hpp"
#include "castor/server/ChildProcess.hpp"
#include "castor/server/Semaphores.hpp"
28
29

#include <gtest/gtest.h>
30
#include <time.h>
31
32
33
34

/* Note: those tests create multi threading errors on purpose and should not
 * be run in helgrind */

35
namespace unitTests {
36
37
38
  TEST(castor_tape_threading, Mutex_properly_throws_exceptions) {
    /* Check that we properly get exception when doing wrong semaphore 
     operations */
39
    castor::server::Mutex m;
40
41
    ASSERT_NO_THROW(m.lock());
    /* Duplicate lock */
42
    ASSERT_THROW(m.lock(),cta::exception::Errnum);
43
44
    ASSERT_NO_THROW(m.unlock());
    /* Duplicate release */
45
    ASSERT_THROW(m.unlock(),cta::exception::Errnum);
46
47
48
  }
  
  TEST(castor_tape_threading, MutexLocker_locks_and_properly_throws_exceptions) {
49
    castor::server::Mutex m;
50
    {
51
      castor::server::MutexLocker ml(&m);
52
      /* This is a different flavourr of duplicate locking */
53
      ASSERT_THROW(m.lock(),cta::exception::Errnum);
54
55
56
57
58
59
60
61
62
63
      ASSERT_NO_THROW(m.unlock());
      ASSERT_NO_THROW(m.lock());
    }
    /* As the locker has been destroyed, and the mutex released, another 
     * lock/unlock should be possible */
    ASSERT_NO_THROW(m.lock());
    ASSERT_NO_THROW(m.unlock());
  }

  TEST(castor_tape_threading, PosixSemaphore_basic_counting) {
64
    castor::server::PosixSemaphore s(2);
65
66
    ASSERT_NO_THROW(s.acquire());
    ASSERT_EQ(true, s.tryAcquire());
Steven Murray's avatar
Steven Murray committed
67
    ASSERT_FALSE(s.tryAcquire());
68
69
70
  }

  TEST(castor_tape_threading, CondVarSemaphore_basic_counting) {
71
    castor::server::CondVarSemaphore s(2);
72
73
    ASSERT_NO_THROW(s.acquire());
    ASSERT_EQ(true, s.tryAcquire());
Steven Murray's avatar
Steven Murray committed
74
    ASSERT_FALSE(s.tryAcquire());
75
76
77
  }

  TEST(castor_tape_threading, Semaphore_basic_counting) {
78
    castor::server::Semaphore s(2);
79
80
    ASSERT_NO_THROW(s.acquire());
    ASSERT_EQ(true, s.tryAcquire());
Steven Murray's avatar
Steven Murray committed
81
    ASSERT_FALSE(s.tryAcquire());
82
83
  }
  
84
  class Thread_exception_throwing: public castor::server::Thread {
85
86
  private:
    void run() {
87
      throw cta::exception::Exception("Exception in child thread");
88
89
90
91
92
93
    }
  };
  TEST(castor_tape_threading, Thread_exception_throwing) {
    Thread_exception_throwing t, t2;
    t.start();
    t2.start();
94
    ASSERT_THROW(t.wait(), castor::server::UncaughtExceptionInThread);
95
96
97
98
99
100
101
    try {
      t2.wait();
    } catch (std::exception & e) {
      std::string w(e.what());
      ASSERT_NE(std::string::npos, w.find("Exception in child thread"));
    }
  }
102
} // namespace unitTests