DiskWriteThreadPoolTest.cpp 6 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
#include "catalogue/DummyCatalogue.hpp"
34
35
36
#include <gtest/gtest.h>

namespace unitTests{
37
  
38
  class TestingDatabaseRetrieveMount: public cta::SchedulerDatabase::RetrieveMount {
39
    const MountInfo & getMountInfo() override { throw std::runtime_error("Not implemented"); }
40
    std::list<std::unique_ptr<cta::SchedulerDatabase::RetrieveJob> > getNextJobBatch(uint64_t filesRequested, uint64_t bytesRequested, cta::disk::DiskSystemFreeSpaceList & diskSystemFreeSpace, cta::log::LogContext& logContext) override { throw std::runtime_error("Not implemented");}
41

42
43
    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"); }
44
    void setTapeSessionStats(const castor::tape::tapeserver::daemon::TapeSessionStats &stats) override { throw std::runtime_error("Not implemented"); }
45
    void flushAsyncSuccessReports(std::list<cta::SchedulerDatabase::RetrieveJob*>& jobsBatch, cta::log::LogContext& lc) override { throw std::runtime_error("Not implemented"); }
46
47
  };
  
48
49
  class TestingRetrieveMount: public cta::RetrieveMount {
  public:
50
    TestingRetrieveMount(cta::catalogue::Catalogue &catalogue, std::unique_ptr<cta::SchedulerDatabase::RetrieveMount> dbrm): RetrieveMount(catalogue, std::move(dbrm)) {
51
52
53
54
55
    }
  };
  
  class TestingRetrieveJob: public cta::RetrieveJob {
  public:
56
    TestingRetrieveJob(): cta::RetrieveJob(nullptr,
57
58
    cta::common::dataStructures::RetrieveRequest(), 
    cta::common::dataStructures::ArchiveFile(), 1,
59
    cta::PositioningMethod::ByBlock) {}
60
61
  };
  
62
63

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

142
143
}