DiskWriteThreadPoolTest.cpp 5.44 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
/****************************************************************************** 
 *
 * 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
 *****************************************************************************/
23
24
25
26
#include "castor/tape/tapeserver/daemon/DiskWriteThreadPool.hpp"
#include "castor/tape/tapeserver/daemon/RecallTaskInjector.hpp"
#include "castor/tape/tapeserver/daemon/RecallReportPacker.hpp"
#include "castor/tape/tapeserver/daemon/ReportPackerInterface.hpp"
Victor Kotlyar's avatar
Victor Kotlyar committed
27
28
#include "common/log/LogContext.hpp"
#include "common/log/StringLogger.hpp"
29
30
#include "castor/tape/tapeserver/daemon/MigrationMemoryManager.hpp"
#include "castor/tape/tapeserver/daemon/MemBlock.hpp"
31
#include "castor/messages/TapeserverProxyDummy.hpp"
32
#include "scheduler/TapeMountDummy.hpp"
33
34
35
#include <gtest/gtest.h>

namespace unitTests{
36
  
37
  class TestingDatabaseRetrieveMount: public cta::SchedulerDatabase::RetrieveMount {
38
39
40
41
    const MountInfo & getMountInfo() override { throw std::runtime_error("Not implemented"); }
    std::unique_ptr<cta::SchedulerDatabase::RetrieveJob> getNextJob(cta::log::LogContext & logContext) override { throw std::runtime_error("Not implemented");}
    void complete(time_t completionTime) override { throw std::runtime_error("Not implemented"); }
    void setDriveStatus(cta::common::dataStructures::DriveStatus status, time_t completionTime) override { throw std::runtime_error("Not implemented"); }
42
    void setTapeSessionStats(castor::tape::tapeserver::daemon::TapeSessionStats stats) override { throw std::runtime_error("Not implemented"); }
43
44
  };
  
45
46
47
48
49
50
51
52
  class TestingRetrieveMount: public cta::RetrieveMount {
  public:
    TestingRetrieveMount(std::unique_ptr<cta::SchedulerDatabase::RetrieveMount> dbrm): RetrieveMount(std::move(dbrm)) {
    }
  };
  
  class TestingRetrieveJob: public cta::RetrieveJob {
  public:
53
    TestingRetrieveJob(): cta::RetrieveJob(*((cta::RetrieveMount *)NULL),
54
55
    cta::common::dataStructures::RetrieveRequest(), 
    cta::common::dataStructures::ArchiveFile(), 1,
56
    cta::PositioningMethod::ByBlock) {}
57
58
  };
  
59
60

  
61
62
  using namespace castor::tape::tapeserver::daemon;
  using namespace castor::tape::tapeserver::client;
63
  struct MockRecallReportPacker : public RecallReportPacker {
64
    void reportCompletedJob(std::unique_ptr<cta::RetrieveJob> successfulRetrieveJob) {
65
      cta::threading::MutexLocker ml(m_mutex);
66
      completeJobs++;
67
    }
68
    void reportFailedJob(std::unique_ptr<cta::RetrieveJob> failedRetrieveJob) {
69
      cta::threading::MutexLocker ml(m_mutex);
70
71
72
73
      failedJobs++;
    }
    void disableBulk() {}
    void reportEndOfSession() {
74
      cta::threading::MutexLocker ml(m_mutex);
75
76
77
      endSessions++;
    }
    void reportEndOfSessionWithErrors(const std::string msg, int error_code) {
78
      cta::threading::MutexLocker ml(m_mutex);
79
      endSessionsWithError++;
80
    }
Victor Kotlyar's avatar
Victor Kotlyar committed
81
    MockRecallReportPacker(cta::RetrieveMount *rm, cta::log::LogContext lc):
82
83
      RecallReportPacker(rm,lc), completeJobs(0), failedJobs(0),
      endSessions(0), endSessionsWithError(0) {}
84
    cta::threading::Mutex m_mutex;
85
86
87
88
    int completeJobs;
    int failedJobs;
    int endSessions;
    int endSessionsWithError;
89
90
  };
  
91
  struct MockTaskInjector : public RecallTaskInjector{
92
93
94
95
96
    MOCK_METHOD3(requestInjection, void(int maxFiles, int maxBlocks, bool lastCall));
  };
  
  TEST(castor_tape_tapeserver_daemon, DiskWriteThreadPoolTest){
    using ::testing::_;
97
    
Victor Kotlyar's avatar
Victor Kotlyar committed
98
99
    cta::log::StringLogger log("castor_tape_tapeserver_daemon_DiskWriteThreadPoolTest",cta::log::DEBUG);
    cta::log::LogContext lc(log);
100
    
101
    std::unique_ptr<cta::SchedulerDatabase::RetrieveMount> dbrm(new TestingDatabaseRetrieveMount);
102
    TestingRetrieveMount trm(std::move(dbrm));
103
    MockRecallReportPacker report(&trm,lc);    
104
    
105
    RecallMemoryManager mm(10,100,lc);
106
    
107
    castor::messages::TapeserverProxyDummy tspd;
108
109
    cta::TapeMountDummy tmd;
    RecallWatchDog rwd(1,1,tspd,tmd,"", lc);
110
    
111
    DiskWriteThreadPool dwtp(2,report,rwd,lc,"/dev/null", 0);
112
113
    dwtp.startThreads();
       
114
115
    for(int i=0;i<5;++i){
      std::unique_ptr<TestingRetrieveJob> fileToRecall(new TestingRetrieveJob());
116
117
118
119
120
      fileToRecall->retrieveRequest.archiveFileID = i+1;
      fileToRecall->retrieveRequest.dstURL = "/dev/null";
      fileToRecall->selectedCopyNb=1;
      fileToRecall->archiveFile.tapeFiles[1];
      fileToRecall->selectedTapeFile().blockId = 1;
121
122
123
124
125
126
127
128
      DiskWriteTask* t=new DiskWriteTask(fileToRecall.release(),mm);
      MemBlock* mb=mm.getFreeBlock();
      mb->m_fileid=i+1;
      mb->m_fileBlock=0;
      t->pushDataBlock(mb);
      t->pushDataBlock(NULL);
      dwtp.push(t);
    }
129
130
     
    dwtp.finish();
131
132
133
    dwtp.waitThreads();
    ASSERT_EQ(5, report.completeJobs);
    ASSERT_EQ(1, report.endSessions);
134
  }
135

136
137
}