1 /*===========================================================================
2 *
3 *                            PUBLIC DOMAIN NOTICE
4 *               National Center for Biotechnology Information
5 *
6 *  This software/database is a "United States Government Work" under the
7 *  terms of the United States Copyright Act.  It was written as part of
8 *  the author's official duties as a United States Government employee and
9 *  thus cannot be copyrighted.  This software/database is freely available
10 *  to the public for use. The National Library of Medicine and the U.S.
11 *  Government have not placed any restriction on its use or reproduction.
12 *
13 *  Although all reasonable efforts have been taken to ensure the accuracy
14 *  and reliability of the software and data, the NLM and the U.S.
15 *  Government do not and cannot warrant the performance or results that
16 *  may be obtained by using this software or data. The NLM and the U.S.
17 *  Government disclaim all warranties, express or implied, including
18 *  warranties of performance, merchantability or fitness for any particular
19 *  purpose.
20 *
21 *  Please cite the author in any work or product based on this material.
22 *
23 * ===========================================================================
24 *
25 */
26 
27 #include <ktst/unit_test.hpp>
28 #include <klib/log.h>
29 #include <vdb/manager.h>
30 #include <vdb/table.h>
31 
32 extern "C" {
33 #include "../../libs/axf/restore-read.h"
34 unsigned RestoreReadShared_getState(unsigned *refSeqs, unsigned *wgs, unsigned *errors, unsigned *activeRefSeqs);
35 }
36 
37 #include <chrono>
38 #include <thread>
39 
40 TEST_SUITE(RestoreReadSuite);
41 
42 class RR_Fixture
43 {
44 public:
45     VDBManager const *mgr;
46     std::string const seqid = "NC_000001.11";
47     uint8_t read[5000];
48 
RR_Fixture()49     RR_Fixture()
50     : mgr(0)
51     {
52         auto const rc = VDBManagerMakeRead(&mgr, 0);
53         assert(rc == 0);
54     }
55 
make()56     RestoreRead *make() {
57         rc_t rc = 0;
58         RestoreRead *rr = RestoreReadMake(mgr, &rc);
59         assert(rc == 0);
60         return rr;
61     }
free(RestoreRead * rr)62     void free(RestoreRead *rr) {
63         RestoreReadFree(rr);
64     }
sequence(RestoreRead * rr,unsigned start)65     unsigned sequence(RestoreRead *rr, unsigned start) {
66         unsigned nread = 0;
67         auto const rc = RestoreReadGetSequence(rr, start, 5000, read, seqid.size(), seqid.c_str(), &nread, nullptr);
68         assert(rc == 0);
69         return nread;
70     }
71 
72     struct State {
73         unsigned refSeqs, wgs, errors, activeRefSeqs;
74         bool isActive;
75     };
status()76     State status() {
77         State result = {};
78         auto active = RestoreReadShared_getState(&result.refSeqs, &result.wgs, &result.errors, &result.activeRefSeqs);
79         result.isActive = active > 0;
80         return result;
81     }
82 };
83 
FIXTURE_TEST_CASE(ReadAllAndClose,RR_Fixture)84 FIXTURE_TEST_CASE(ReadAllAndClose, RR_Fixture)
85 {
86     {
87         auto const &rr = make();
88         unsigned pos = 0;
89         unsigned nread = 0;
90         bool done = false;
91 
92         REQUIRE_EQ(5000u, nread = sequence(rr, pos));
93         {
94             auto const &state = status();
95             REQUIRE(state.isActive);
96             REQUIRE_EQ(state.refSeqs, 1u);
97             REQUIRE_EQ(state.activeRefSeqs, 1u);
98             REQUIRE_EQ(state.wgs, 0u);
99             REQUIRE_EQ(state.errors, 0u);
100         }
101         std::this_thread::sleep_for(std::chrono::seconds(1));
102         pos += nread;
103         while ((nread = sequence(rr, pos)) > 0) {
104             pos += nread;
105             done = false;
106             {
107                 auto const &state = status();
108                 if (!state.isActive)
109                     break;
110                 if (state.activeRefSeqs == 0)
111                     break;
112                 std::this_thread::sleep_for(std::chrono::milliseconds(1));
113             }
114             done = true;
115         }
116         if (!done) {
117             LOGMSG(klogDebug, "Continuing without waiting");
118             while ((nread = sequence(rr, pos)) > 0) {
119                 pos += nread;
120             }
121             for ( ; ; ) {
122                 auto const &state = status();
123                 if (!state.isActive)
124                     break;
125                 if (state.activeRefSeqs == 0)
126                     break;
127                 std::this_thread::sleep_for(std::chrono::milliseconds(100));
128             }
129         }
130         free(rr);
131     }
132 }
133 
FIXTURE_TEST_CASE(ReadSomeAndClose,RR_Fixture)134 FIXTURE_TEST_CASE(ReadSomeAndClose, RR_Fixture)
135 {
136     {
137         auto const &rr = make();
138 
139         REQUIRE_EQ(5000u, sequence(rr, 0));
140         {
141             auto const &state = status();
142             REQUIRE(state.isActive);
143             REQUIRE_EQ(state.refSeqs, 1u);
144             REQUIRE_EQ(state.wgs, 0u);
145             REQUIRE_EQ(state.errors, 0u);
146         }
147 
148         free(rr);
149     }
150     {
151         auto const &state = status();
152         REQUIRE(!state.isActive);
153     }
154 }
155 
156 //////////////////////////////////////////// Main
157 extern "C"
158 {
159 
160 #include <kapp/args.h>
161 #include <kfg/config.h>
162 
KAppVersion(void)163 ver_t CC KAppVersion ( void )
164 {
165     return 0x1000000;
166 }
UsageSummary(const char * progname)167 rc_t CC UsageSummary (const char * progname)
168 {
169     return 0;
170 }
171 
Usage(const Args * args)172 rc_t CC Usage ( const Args * args )
173 {
174     return 0;
175 }
176 
177 const char UsageDefaultName[] = "test-RestoreRead";
178 
KMain(int argc,char * argv[])179 rc_t CC KMain ( int argc, char *argv [] )
180 {
181     KConfigDisableUserSettings();
182     // KLogLevelSet(klogDebug);
183     rc_t rc = RestoreReadSuite(argc, argv);
184     return rc;
185 }
186 
187 }
188