|
| 1 | +/* |
| 2 | + * Copyright (C) 2012,2013 tamtam180 |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.arangodb; |
| 18 | + |
| 19 | +import static org.junit.Assert.assertEquals; |
| 20 | +import static org.junit.Assert.assertNotNull; |
| 21 | +import static org.junit.Assert.assertTrue; |
| 22 | + |
| 23 | +import java.util.HashMap; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Map; |
| 26 | + |
| 27 | +import org.junit.Before; |
| 28 | +import org.junit.Test; |
| 29 | + |
| 30 | +import com.arangodb.entity.DefaultEntity; |
| 31 | +import com.arangodb.entity.QueriesResultEntity; |
| 32 | +import com.arangodb.entity.QueryEntity; |
| 33 | +import com.arangodb.entity.QueryTrackingPropertiesEntity; |
| 34 | + |
| 35 | +/** |
| 36 | + * @author a-brandt |
| 37 | + * |
| 38 | + */ |
| 39 | +public class ArangoDriverQueryTest extends BaseTest { |
| 40 | + |
| 41 | + public ArangoDriverQueryTest(ArangoConfigure configure, ArangoDriver driver) { |
| 42 | + super(configure, driver); |
| 43 | + } |
| 44 | + |
| 45 | + @Before |
| 46 | + public void setup() throws ArangoException { |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void test_getQueryTrackingProperties() throws ArangoException { |
| 51 | + QueryTrackingPropertiesEntity queryTrackingProperties = driver.getQueryTrackingProperties(); |
| 52 | + assertEquals(200, queryTrackingProperties.getStatusCode()); |
| 53 | + assertNotNull(queryTrackingProperties.getEnabled()); |
| 54 | + assertNotNull(queryTrackingProperties.getTrackSlowQueries()); |
| 55 | + assertNotNull(queryTrackingProperties.getSlowQueryThreshold()); |
| 56 | + assertNotNull(queryTrackingProperties.getMaxSlowQueries()); |
| 57 | + assertNotNull(queryTrackingProperties.getMaxQueryStringLength()); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void test_setQueryTrackingProperties() throws ArangoException { |
| 62 | + QueryTrackingPropertiesEntity properties1 = driver.getQueryTrackingProperties(); |
| 63 | + |
| 64 | + Long maxQueryStringLength = properties1.getMaxQueryStringLength() + 10; |
| 65 | + |
| 66 | + properties1.setMaxQueryStringLength(maxQueryStringLength); |
| 67 | + |
| 68 | + QueryTrackingPropertiesEntity properties2 = driver.setQueryTrackingProperties(properties1); |
| 69 | + |
| 70 | + assertEquals(200, properties2.getStatusCode()); |
| 71 | + assertNotNull(properties2.getEnabled()); |
| 72 | + assertNotNull(properties2.getTrackSlowQueries()); |
| 73 | + assertNotNull(properties2.getSlowQueryThreshold()); |
| 74 | + assertNotNull(properties2.getMaxSlowQueries()); |
| 75 | + assertNotNull(properties2.getMaxQueryStringLength()); |
| 76 | + assertEquals(maxQueryStringLength, properties2.getMaxQueryStringLength()); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void test_getCurrentlyRunningQueries() throws ArangoException, InterruptedException { |
| 81 | + |
| 82 | + String queryString = "return sleep(3)"; |
| 83 | + |
| 84 | + Thread thread1 = new Thread(new RunnableThread(driver.getDefaultDatabase(), configure, queryString), "thread1"); |
| 85 | + thread1.start(); |
| 86 | + |
| 87 | + Thread.sleep(1000); |
| 88 | + |
| 89 | + QueriesResultEntity currentlyRunningQueries = driver.getCurrentlyRunningQueries(); |
| 90 | + |
| 91 | + // wait for thread |
| 92 | + thread1.join(); |
| 93 | + |
| 94 | + // check result |
| 95 | + assertEquals(200, currentlyRunningQueries.getStatusCode()); |
| 96 | + List<QueryEntity> queries = currentlyRunningQueries.getQueries(); |
| 97 | + assertEquals(1, queries.size()); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void test_getCurrentlyRunningQueriesWithoutDatabase() throws ArangoException, InterruptedException { |
| 102 | + |
| 103 | + String queryString = "return sleep(3)"; |
| 104 | + |
| 105 | + // create job in _system database |
| 106 | + Thread thread1 = new Thread(new RunnableThread(null, configure, queryString), "thread1"); |
| 107 | + thread1.start(); |
| 108 | + |
| 109 | + Thread.sleep(1000); |
| 110 | + |
| 111 | + // search in default database |
| 112 | + QueriesResultEntity currentlyRunningQueries = driver.getCurrentlyRunningQueries(); |
| 113 | + |
| 114 | + // wait for thread |
| 115 | + thread1.join(); |
| 116 | + |
| 117 | + // check result |
| 118 | + assertEquals(200, currentlyRunningQueries.getStatusCode()); |
| 119 | + List<QueryEntity> queries = currentlyRunningQueries.getQueries(); |
| 120 | + assertEquals(0, queries.size()); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + public void test_getCurrentlyRunningQueriesWithoutDatabase2() throws ArangoException, InterruptedException { |
| 125 | + |
| 126 | + String queryString = "return sleep(3)"; |
| 127 | + |
| 128 | + // create job in _system database |
| 129 | + Thread thread1 = new Thread(new RunnableThread(null, configure, queryString), "thread1"); |
| 130 | + thread1.start(); |
| 131 | + |
| 132 | + Thread.sleep(1000); |
| 133 | + |
| 134 | + // search in _system database |
| 135 | + QueriesResultEntity currentlyRunningQueries = driver.getCurrentlyRunningQueries(null); |
| 136 | + |
| 137 | + // wait for thread |
| 138 | + thread1.join(); |
| 139 | + |
| 140 | + // check result |
| 141 | + assertEquals(200, currentlyRunningQueries.getStatusCode()); |
| 142 | + List<QueryEntity> queries = currentlyRunningQueries.getQueries(); |
| 143 | + assertEquals(1, queries.size()); |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + public void test_getSlowQueries() throws ArangoException, InterruptedException { |
| 148 | + // set SlowQueryThreshold to 2 |
| 149 | + QueryTrackingPropertiesEntity properties1 = driver.getQueryTrackingProperties(); |
| 150 | + properties1.setSlowQueryThreshold(2L); |
| 151 | + driver.setQueryTrackingProperties(properties1); |
| 152 | + |
| 153 | + // create a slow query |
| 154 | + driver.executeDocumentQuery("return sleep(3)", new HashMap<String, Object>(), null, Map.class); |
| 155 | + |
| 156 | + QueriesResultEntity currentlyRunningQueries = driver.getSlowQueries(); |
| 157 | + |
| 158 | + // check result |
| 159 | + assertEquals(200, currentlyRunningQueries.getStatusCode()); |
| 160 | + List<QueryEntity> queries = currentlyRunningQueries.getQueries(); |
| 161 | + assertTrue(queries.size() > 0); |
| 162 | + } |
| 163 | + |
| 164 | + @Test |
| 165 | + public void test_deleteSlowQueries() throws ArangoException, InterruptedException { |
| 166 | + driver.deleteSlowQueries(); |
| 167 | + QueriesResultEntity currentlyRunningQueries = driver.getSlowQueries(); |
| 168 | + |
| 169 | + // check result |
| 170 | + assertEquals(200, currentlyRunningQueries.getStatusCode()); |
| 171 | + List<QueryEntity> queries = currentlyRunningQueries.getQueries(); |
| 172 | + assertEquals(0, queries.size()); |
| 173 | + |
| 174 | + // set SlowQueryThreshold to 2 |
| 175 | + QueryTrackingPropertiesEntity properties1 = driver.getQueryTrackingProperties(); |
| 176 | + properties1.setSlowQueryThreshold(2L); |
| 177 | + driver.setQueryTrackingProperties(properties1); |
| 178 | + |
| 179 | + // create a slow query |
| 180 | + driver.executeDocumentQuery("return sleep(3)", new HashMap<String, Object>(), null, Map.class); |
| 181 | + |
| 182 | + currentlyRunningQueries = driver.getSlowQueries(); |
| 183 | + |
| 184 | + // check result |
| 185 | + assertEquals(200, currentlyRunningQueries.getStatusCode()); |
| 186 | + queries = currentlyRunningQueries.getQueries(); |
| 187 | + assertEquals(1, queries.size()); |
| 188 | + } |
| 189 | + |
| 190 | + @Test |
| 191 | + public void test_killQuery() throws ArangoException, InterruptedException { |
| 192 | + |
| 193 | + String queryString = "return sleep(3)"; |
| 194 | + |
| 195 | + // create job in _system database |
| 196 | + Thread thread1 = new Thread(new RunnableThread(driver.getDefaultDatabase(), configure, queryString), "thread1"); |
| 197 | + thread1.start(); |
| 198 | + |
| 199 | + Thread.sleep(1000); |
| 200 | + |
| 201 | + int numKilled = 0; |
| 202 | + |
| 203 | + // search in default database |
| 204 | + QueriesResultEntity currentlyRunningQueries = driver.getCurrentlyRunningQueries(); |
| 205 | + // check result |
| 206 | + assertEquals(200, currentlyRunningQueries.getStatusCode()); |
| 207 | + List<QueryEntity> queries = currentlyRunningQueries.getQueries(); |
| 208 | + if (queries.size() > 0) { |
| 209 | + for (QueryEntity qe : queries) { |
| 210 | + try { |
| 211 | + DefaultEntity killQuery = driver.killQuery(qe.getId()); |
| 212 | + assertEquals(200, killQuery.getStatusCode()); |
| 213 | + numKilled++; |
| 214 | + } catch (ArangoException e) { |
| 215 | + |
| 216 | + } |
| 217 | + } |
| 218 | + } |
| 219 | + |
| 220 | + // wait for thread |
| 221 | + thread1.join(); |
| 222 | + assertTrue(queries.size() > 0); |
| 223 | + assertEquals(numKilled, queries.size()); |
| 224 | + } |
| 225 | + |
| 226 | + class RunnableThread implements Runnable { |
| 227 | + |
| 228 | + Thread runner; |
| 229 | + ArangoDriver driver; |
| 230 | + String queryString; |
| 231 | + |
| 232 | + public RunnableThread(String database, ArangoConfigure configure, String queryString) { |
| 233 | + this.driver = new ArangoDriver(configure); |
| 234 | + this.driver.setDefaultDatabase(database); |
| 235 | + this.queryString = queryString; |
| 236 | + } |
| 237 | + |
| 238 | + public RunnableThread(String threadName) { |
| 239 | + runner = new Thread(this, threadName); |
| 240 | + runner.start(); |
| 241 | + } |
| 242 | + |
| 243 | + public void run() { |
| 244 | + try { |
| 245 | + driver.executeDocumentQuery(queryString, new HashMap<String, Object>(), null, Map.class); |
| 246 | + } catch (ArangoException e) { |
| 247 | + if (e.getErrorNumber() != ErrorNums.ERROR_QUERY_KILLED) { |
| 248 | + e.printStackTrace(); |
| 249 | + } |
| 250 | + } |
| 251 | + |
| 252 | + } |
| 253 | + } |
| 254 | +} |
0 commit comments