1+ /*
2+ * Copyright 2019 Philipp Salvisberg <philipp.salvisberg@trivadis.com>
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+ package org.utplsql.sqldev.test.dal
17+
18+ import java.util.UUID
19+ import java.util.logging.Logger
20+ import org.junit.AfterClass
21+ import org.junit.Assert
22+ import org.junit.BeforeClass
23+ import org.junit.Test
24+ import org.springframework.jdbc.BadSqlGrammarException
25+ import org.springframework.jdbc.datasource.SingleConnectionDataSource
26+ import org.utplsql.sqldev.dal.RealtimeReporterDao
27+ import org.utplsql.sqldev.test.AbstractJdbcTest
28+
29+ class RealtimeReporterFetchSizeTest extends AbstractJdbcTest {
30+
31+ static val Logger logger = Logger . getLogger(RealtimeReporterFetchSizeTest . name);
32+
33+ @BeforeClass
34+ def static void setup () {
35+
36+ jdbcTemplate. execute(' ' '
37+ CREATE OR REPLACE PACKAGE junit_utplsql_fetch_size_pkg is
38+ --%suite(JUnit testing)
39+
40+ --%test(test 1 - 0 seconds)
41+ PROCEDURE test_1_0;
42+
43+ --%test(test 2 - 1 seconds)
44+ PROCEDURE test_2_1;
45+
46+ --%test(test 3 - 2 seconds)
47+ PROCEDURE test_3_2;
48+
49+ --%test(test 4 - 0 seconds)
50+ PROCEDURE test_4_0;
51+
52+ --%test(test 5 - 0 seconds)
53+ PROCEDURE test_5_0;
54+ END;
55+ ' ' ' )
56+ jdbcTemplate. execute(' ' '
57+ CREATE OR REPLACE PACKAGE BODY junit_utplsql_fetch_size_pkg is
58+ PROCEDURE test_1_0 IS
59+ BEGIN
60+ NULL;
61+ END;
62+
63+ PROCEDURE test_2_1 IS
64+ BEGIN
65+ dbms_session.sleep(1);
66+ END;
67+
68+ PROCEDURE test_3_2 IS
69+ BEGIN
70+ dbms_session.sleep(2);
71+ END;
72+
73+ PROCEDURE test_4_0 IS
74+ BEGIN
75+ NULL;
76+ END;
77+
78+ PROCEDURE test_5_0 IS
79+ BEGIN
80+ NULL;
81+ END;
82+ END;
83+ ' ' ' )
84+ }
85+
86+ @AfterClass
87+ def static void teardown () {
88+ try {
89+ jdbcTemplate. execute(" DROP PACKAGE junit_utplsql_fetch_size_pkg" )
90+ } catch (BadSqlGrammarException e) {
91+ // ignore
92+ }
93+ }
94+
95+ private def delayFreeStreamingConsumtionProducer (String reporterId ) {
96+ var ds = new SingleConnectionDataSource ()
97+ ds. driverClassName = " oracle.jdbc.OracleDriver"
98+ ds. url = dataSource. url
99+ ds. username = dataSource. username
100+ ds. password = dataSource. password
101+ val dao = new RealtimeReporterDao (ds. connection)
102+ dao. produceReport(reporterId, #[" junit_utplsql_fetch_size_pkg" ])
103+ }
104+
105+ @Test
106+ def void delayFreeStreamingConsumtion () {
107+ val long TOLERANCE_MS = 400
108+ var ds = new SingleConnectionDataSource ()
109+ ds. driverClassName = " oracle.jdbc.OracleDriver"
110+ ds. url = dataSource. url
111+ ds. username = dataSource. username
112+ ds. password = dataSource. password
113+ val consumer = new TestRealtimerReporterEventTimedConsumer
114+ val reporterId = UUID . randomUUID(). toString. replace(" -" , " " );
115+ val dao = new RealtimeReporterDao (ds. connection)
116+ val Runnable runnable = [|delayFreeStreamingConsumtionProducer(reporterId)]
117+ val thread = new Thread (runnable)
118+ thread. name = " utPLSQL run test"
119+ thread. start
120+ dao. consumeReport(reporterId, consumer)
121+ logger. fine(consumer. postTestEvents. toString)
122+ Assert . assertEquals(5 , consumer. postTestEvents. entrySet. size)
123+ val test_1_0 = consumer. postTestEvents. get(" junit_utplsql_fetch_size_pkg.test_1_0" )
124+ val test_2_1 = consumer. postTestEvents. get(" junit_utplsql_fetch_size_pkg.test_2_1" )
125+ val test_3_2 = consumer. postTestEvents. get(" junit_utplsql_fetch_size_pkg.test_3_2" )
126+ val test_4_0 = consumer. postTestEvents. get(" junit_utplsql_fetch_size_pkg.test_4_0" )
127+ val test_5_0 = consumer. postTestEvents. get(" junit_utplsql_fetch_size_pkg.test_5_0" )
128+ val test_2_1_time = test_2_1 - test_1_0
129+ logger. fine(" test_2_1 time [ms]: " + test_2_1_time)
130+ Assert . assertTrue(" test_2_1 runtime was too long" , test_2_1_time < 1000 + TOLERANCE_MS )
131+ Assert . assertTrue(" test_2_1 runtime was too short" , test_2_1_time > 1000 - TOLERANCE_MS )
132+ val test_3_2_time = test_3_2 - test_2_1
133+ logger. fine(" test_3_2 time [ms]: " + test_3_2_time)
134+ Assert . assertTrue(" test_3_2 runtime was too long" , test_3_2_time < 2000 + TOLERANCE_MS )
135+ Assert . assertTrue(" test_3_2 runtime was too short" , test_3_2_time > 2000 - TOLERANCE_MS )
136+ val test_4_0_time = test_4_0 - test_3_2
137+ logger. fine(" test_4_0 time [ms]: " + test_4_0_time)
138+ Assert . assertTrue(" test_4_0 runtime was too long" , test_4_0_time < TOLERANCE_MS )
139+ val test_5_0_time = test_5_0 - test_4_0
140+ logger. fine(" test_5_0 time [ms]: " + test_5_0_time)
141+ Assert . assertTrue(" test_5_0 runtime was too long" , test_5_0_time < TOLERANCE_MS )
142+ }
143+
144+ }
0 commit comments