1 /** @file
2 
3   A brief file description
4 
5   @section license License
6 
7   Licensed to the Apache Software Foundation (ASF) under one
8   or more contributor license agreements.  See the NOTICE file
9   distributed with this work for additional information
10   regarding copyright ownership.  The ASF licenses this file
11   to you under the Apache License, Version 2.0 (the
12   "License"); you may not use this file except in compliance
13   with the License.  You may obtain a copy of the License at
14 
15       http://www.apache.org/licenses/LICENSE-2.0
16 
17   Unless required by applicable law or agreed to in writing, software
18   distributed under the License is distributed on an "AS IS" BASIS,
19   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   See the License for the specific language governing permissions and
21   limitations under the License.
22  */
23 
24 #define LARGE_FILE 10 * 1024 * 1024
25 #define SMALL_FILE 10 * 1024
26 
27 #include "main.h"
28 
29 class CacheUpdateReadAgain : public CacheTestHandler
30 {
31 public:
CacheUpdateReadAgain(size_t size,const char * url)32   CacheUpdateReadAgain(size_t size, const char *url) : CacheTestHandler()
33   {
34     this->_rt        = new CacheReadTest(size, this, url);
35     this->_rt->mutex = this->mutex;
36 
37     SET_HANDLER(&CacheUpdateReadAgain::start_test);
38   }
39 
40   int
start_test(int event,void * e)41   start_test(int event, void *e)
42   {
43     REQUIRE(event == EVENT_IMMEDIATE);
44     this_ethread()->schedule_imm(this->_rt);
45     return 0;
46   }
47 
48   void
handle_cache_event(int event,CacheTestBase * base)49   handle_cache_event(int event, CacheTestBase *base) override
50   {
51     switch (event) {
52     case CACHE_EVENT_OPEN_READ:
53       base->do_io_read();
54       break;
55     case VC_EVENT_READ_READY:
56       base->reenable();
57       break;
58     case VC_EVENT_READ_COMPLETE:
59       base->close();
60       delete this;
61       break;
62     default:
63       REQUIRE(false);
64       break;
65     }
66   }
67 };
68 
69 class CacheUpdate_S_to_L : public CacheTestHandler
70 {
71 public:
CacheUpdate_S_to_L(size_t read_size,size_t write_size,const char * url)72   CacheUpdate_S_to_L(size_t read_size, size_t write_size, const char *url)
73   {
74     this->_rt = new CacheReadTest(read_size, this, url);
75     this->_wt = new CacheWriteTest(write_size, this, url);
76 
77     this->_rt->mutex = this->mutex;
78     this->_wt->mutex = this->mutex;
79 
80     SET_HANDLER(&CacheUpdate_S_to_L::start_test);
81   }
82 
83   int
start_test(int event,void * e)84   start_test(int event, void *e)
85   {
86     REQUIRE(event == EVENT_IMMEDIATE);
87     this_ethread()->schedule_imm(this->_rt);
88     return 0;
89   }
90 
91   void
handle_cache_event(int event,CacheTestBase * base)92   handle_cache_event(int event, CacheTestBase *base) override
93   {
94     CacheWriteTest *wt = static_cast<CacheWriteTest *>(this->_wt);
95     switch (event) {
96     case CACHE_EVENT_OPEN_WRITE:
97       base->do_io_write();
98       break;
99     case VC_EVENT_WRITE_READY:
100       base->reenable();
101       break;
102     case VC_EVENT_WRITE_COMPLETE:
103       this->_wt->close();
104       this->_wt = nullptr;
105       delete this;
106       break;
107     case CACHE_EVENT_OPEN_READ:
108       base->do_io_read();
109       wt->old_info.copy(static_cast<HTTPInfo *>(&base->vc->alternate));
110       break;
111     case VC_EVENT_READ_READY:
112       base->reenable();
113       break;
114     case VC_EVENT_READ_COMPLETE:
115       this->_rt->close();
116       this->_rt = nullptr;
117       this_ethread()->schedule_imm(this->_wt);
118       break;
119     default:
120       REQUIRE(false);
121       break;
122     }
123   }
124 };
125 
126 class CacheUpdateInit : public CacheInit
127 {
128 public:
CacheUpdateInit()129   CacheUpdateInit() {}
130   int
cache_init_success_callback(int event,void * e)131   cache_init_success_callback(int event, void *e) override
132   {
133     CacheTestHandler *h        = new CacheTestHandler(SMALL_FILE, "http://www.scw11.com");
134     CacheUpdate_S_to_L *update = new CacheUpdate_S_to_L(SMALL_FILE, LARGE_FILE, "http://www.scw11.com");
135     CacheUpdateReadAgain *read = new CacheUpdateReadAgain(LARGE_FILE, "http://www.scw11.com");
136     TerminalTest *tt           = new TerminalTest;
137 
138     h->add(update);
139     h->add(read); // read again
140     h->add(tt);
141     this_ethread()->schedule_imm(h);
142     delete this;
143     return 0;
144   }
145 };
146 
147 TEST_CASE("cache write -> read", "cache")
148 {
149   init_cache(256 * 1024 * 1024);
150   // large write test
151   CacheUpdateInit *init = new CacheUpdateInit;
152 
153   this_ethread()->schedule_imm(init);
154   this_thread()->execute();
155 }
156