Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
1e09a772
提交
1e09a772
authored
15 年前
作者:
Thomas Mueller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Simplify testing
上级
efcfb970
显示空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
226 行增加
和
4 行删除
+226
-4
Database.java
h2/src/main/org/h2/engine/Database.java
+1
-0
TestPowerOffFs.java
h2/src/test/org/h2/test/synth/TestPowerOffFs.java
+0
-4
TestPowerOffFs2.java
h2/src/test/org/h2/test/synth/TestPowerOffFs2.java
+225
-0
没有找到文件。
h2/src/main/org/h2/engine/Database.java
浏览文件 @
1e09a772
...
...
@@ -2251,6 +2251,7 @@ public class Database implements DataHandler {
}
catch
(
SQLException
e
)
{
// ignore
}
closeFiles
();
}
public
TempFileDeleter
getTempFileDeleter
()
{
...
...
This diff is collapsed.
Click to expand it.
h2/src/test/org/h2/test/synth/TestPowerOffFs.java
浏览文件 @
1e09a772
...
...
@@ -32,7 +32,6 @@ public class TestPowerOffFs extends TestBase {
}
public
void
test
()
throws
Exception
{
int
todo
;
FileSystemDebug
.
register
();
fs
=
(
FileSystemDebug
)
FileSystem
.
getInstance
(
"debug:/"
);
test
(
Integer
.
MAX_VALUE
);
...
...
@@ -53,9 +52,6 @@ public class TestPowerOffFs extends TestBase {
url
+=
";page_store=true"
;
Connection
conn
=
null
;
Statement
stat
=
null
;
if
(
x
==
16
)
{
System
.
out
.
println
(
"x"
);
}
try
{
conn
=
DriverManager
.
getConnection
(
url
);
stat
=
conn
.
createStatement
();
...
...
This diff is collapsed.
Click to expand it.
h2/src/test/org/h2/test/synth/TestPowerOffFs2.java
0 → 100644
浏览文件 @
1e09a772
/*
* Copyright 2004-2009 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package
org
.
h2
.
test
.
synth
;
import
java.sql.Connection
;
import
java.sql.DriverManager
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
import
java.sql.Statement
;
import
java.util.ArrayList
;
import
java.util.Random
;
import
org.h2.constant.ErrorCode
;
import
org.h2.store.fs.FileSystem
;
import
org.h2.test.TestBase
;
import
org.h2.test.utils.FileSystemDebug
;
import
org.h2.util.New
;
/**
* Tests that use the debug file system to simulate power failure.
* This test runs many random operations and stops after some time.
*/
public
class
TestPowerOffFs2
extends
TestBase
{
private
FileSystemDebug
fs
;
private
String
url
;
private
String
user
=
"sa"
;
private
String
password
=
"sa"
;
private
ArrayList
<
Connection
>
connections
=
New
.
arrayList
();
private
ArrayList
<
String
>
tables
=
New
.
arrayList
();
private
int
openCount
;
/**
* Run just this test.
*
* @param a ignored
*/
public
static
void
main
(
String
...
a
)
throws
Exception
{
TestBase
.
createCaller
().
init
().
test
();
}
public
void
test
()
throws
Exception
{
FileSystemDebug
.
register
();
url
=
"jdbc:h2:debug:memFS:powerOffFs;FILE_LOCK=NO;TRACE_LEVEL_FILE=0;WRITE_DELAY=0;CACHE_SIZE=32"
;
url
+=
";page_store=true"
;
fs
=
(
FileSystemDebug
)
FileSystem
.
getInstance
(
"debug:/"
);
for
(
int
i
=
0
;;
i
++)
{
test
(
i
);
}
}
private
void
test
(
int
x
)
throws
SQLException
{
System
.
out
.
println
(
"x:"
+
x
);
deleteDb
(
"memFS:"
,
null
);
try
{
testCrash
(
x
);
fail
();
}
catch
(
SQLException
e
)
{
if
(
e
.
toString
().
indexOf
(
"Simulated"
)
<
0
)
{
throw
e
;
}
for
(
Connection
c
:
connections
)
{
try
{
Statement
stat
=
c
.
createStatement
();
stat
.
execute
(
"shutdown immediately"
);
}
catch
(
Exception
e2
)
{
// ignore
}
try
{
c
.
close
();
}
catch
(
Exception
e2
)
{
// ignore
}
}
}
fs
.
setPowerOffCount
(
0
);
Connection
conn
;
conn
=
openConnection
();
testConsistent
(
conn
);
conn
.
close
();
}
private
void
testCrash
(
int
x
)
throws
SQLException
{
connections
.
clear
();
tables
.
clear
();
openCount
=
0
;
Random
random
=
new
Random
(
x
);
for
(
int
i
=
0
;;
i
++)
{
if
(
i
>
200
&&
connections
.
size
()
>
1
&&
tables
.
size
()
>
1
)
{
fs
.
setPowerOffCount
(
100
);
}
if
(
connections
.
size
()
<
1
)
{
openConnection
();
}
if
(
tables
.
size
()
<
1
)
{
createTable
(
random
);
}
int
p
=
random
.
nextInt
(
100
);
if
((
p
-=
2
)
<=
0
)
{
// 2%: open new connection
if
(
connections
.
size
()
<
5
)
{
openConnection
();
}
}
else
if
((
p
-=
1
)
<=
0
)
{
// 1%: close connection
if
(
connections
.
size
()
>
1
)
{
Connection
conn
=
connections
.
remove
(
random
.
nextInt
(
connections
.
size
()));
conn
.
close
();
}
}
else
if
((
p
-=
10
)
<=
0
)
{
// 10% create table
createTable
(
random
);
}
else
if
((
p
-=
20
)
<=
0
)
{
// 20% large insert, delete, or update
if
(
tables
.
size
()
>
0
)
{
Connection
conn
=
connections
.
get
(
random
.
nextInt
(
connections
.
size
()));
Statement
stat
=
conn
.
createStatement
();
String
table
=
tables
.
get
(
random
.
nextInt
(
tables
.
size
()));
if
(
random
.
nextBoolean
())
{
// 10% insert
stat
.
execute
(
"INSERT INTO "
+
table
+
"(NAME) SELECT 'Hello ' || X FROM SYSTEM_RANGE(0, 20)"
);
}
else
if
(
random
.
nextBoolean
())
{
// 5% update
stat
.
execute
(
"UPDATE "
+
table
+
" SET NAME='Hallo Welt'"
);
}
else
{
// 5% delete
stat
.
execute
(
"DELETE FROM "
+
table
);
}
}
}
else
if
((
p
-=
5
)
<
0
)
{
// 5% truncate or drop table
if
(
tables
.
size
()
>
0
)
{
Connection
conn
=
connections
.
get
(
random
.
nextInt
(
connections
.
size
()));
Statement
stat
=
conn
.
createStatement
();
String
table
=
tables
.
get
(
random
.
nextInt
(
tables
.
size
()));
if
(
random
.
nextBoolean
())
{
stat
.
execute
(
"TRUNCATE TABLE "
+
table
);
}
else
{
stat
.
execute
(
"DROP TABLE "
+
table
);
tables
.
remove
(
table
);
}
}
}
else
if
((
p
-=
30
)
<=
0
)
{
// 30% insert
if
(
tables
.
size
()
>
0
)
{
Connection
conn
=
connections
.
get
(
random
.
nextInt
(
connections
.
size
()));
Statement
stat
=
conn
.
createStatement
();
String
table
=
tables
.
get
(
random
.
nextInt
(
tables
.
size
()));
int
spaces
=
random
.
nextInt
(
4
)
*
30
;
if
(
random
.
nextInt
(
15
)
==
2
)
{
spaces
*=
100
;
}
int
name
=
random
.
nextInt
(
20
);
stat
.
execute
(
"INSERT INTO "
+
table
+
"(NAME) VALUES('"
+
name
+
"' || space( "
+
spaces
+
" ))"
);
}
}
else
{
// 32% delete
if
(
tables
.
size
()
>
0
)
{
Connection
conn
=
connections
.
get
(
random
.
nextInt
(
connections
.
size
()));
Statement
stat
=
conn
.
createStatement
();
String
table
=
tables
.
get
(
random
.
nextInt
(
tables
.
size
()));
stat
.
execute
(
"DELETE FROM "
+
table
+
" WHERE ID = SELECT MIN(ID) FROM "
+
table
);
}
}
}
}
private
Connection
openConnection
()
throws
SQLException
{
openCount
++;
Connection
conn
=
DriverManager
.
getConnection
(
url
,
user
,
password
);
connections
.
add
(
conn
);
return
conn
;
}
private
void
createTable
(
Random
random
)
throws
SQLException
{
Connection
conn
=
connections
.
get
(
random
.
nextInt
(
connections
.
size
()));
Statement
stat
=
conn
.
createStatement
();
String
table
=
"TEST"
+
random
.
nextInt
(
10
);
try
{
stat
.
execute
(
"CREATE TABLE "
+
table
+
"(ID IDENTITY, NAME VARCHAR)"
);
if
(
random
.
nextBoolean
())
{
stat
.
execute
(
"CREATE INDEX IDX_"
+
table
+
" ON "
+
table
+
"(NAME)"
);
}
tables
.
add
(
table
);
}
catch
(
SQLException
e
)
{
if
(
e
.
getErrorCode
()
==
ErrorCode
.
TABLE_OR_VIEW_ALREADY_EXISTS_1
)
{
if
(!
tables
.
contains
(
table
))
{
tables
.
add
(
table
);
}
// ok
}
else
{
throw
e
;
}
}
}
private
void
testConsistent
(
Connection
conn
)
throws
SQLException
{
for
(
int
i
=
0
;
i
<
20
;
i
++)
{
Statement
stat
=
conn
.
createStatement
();
try
{
ResultSet
rs
=
stat
.
executeQuery
(
"SELECT * FROM TEST"
+
i
);
while
(
rs
.
next
())
{
rs
.
getLong
(
"ID"
);
rs
.
getString
(
"NAME"
);
}
rs
=
stat
.
executeQuery
(
"SELECT * FROM TEST"
+
i
+
" ORDER BY ID"
);
while
(
rs
.
next
())
{
rs
.
getLong
(
"ID"
);
rs
.
getString
(
"NAME"
);
}
}
catch
(
SQLException
e
)
{
if
(
e
.
getErrorCode
()
==
ErrorCode
.
TABLE_OR_VIEW_NOT_FOUND_1
)
{
// ok
}
else
{
throw
e
;
}
}
}
}
}
This diff is collapsed.
Click to expand it.
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论