1/*  Part of SWI-Prolog
2
3    Author:        Jan Wielemaker
4    E-mail:        J.Wielemaker@vu.nl
5    WWW:           www.swi-prolog.org
6    Copyright (c)  2014, University of Amsterdam
7                         VU University Amsterdam
8    All rights reserved.
9
10    Redistribution and use in source and binary forms, with or without
11    modification, are permitted provided that the following conditions
12    are met:
13
14    1. Redistributions of source code must retain the above copyright
15       notice, this list of conditions and the following disclaimer.
16
17    2. Redistributions in binary form must reproduce the above copyright
18       notice, this list of conditions and the following disclaimer in
19       the documentation and/or other materials provided with the
20       distribution.
21
22    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33    POSSIBILITY OF SUCH DAMAGE.
34*/
35
36:- module(test_tmp_module,
37	  [ test_tmp_module/0,
38	    test_tmp_module/1
39	  ]).
40:- use_module(library(modules)).
41:- use_module(library(thread)).
42:- use_module(library(plunit)).
43
44test_tmp_module :-
45	run_tests([ tmp_module
46		  ]).
47
48:- begin_tests(tmp_module).
49
50test(nqueens, condition(current_prolog_flag(threads,true))) :-
51	test_tmp_module(100).
52test(current_op, condition(current_prolog_flag(threads,true))) :-
53	forall(between(1, 100, _),
54	       test_op).
55
56:- end_tests(tmp_module).
57
58%%	test_tmp_module(+Concurrent)
59%
60%	Test  concurrent  loading  of  nqueens   in  multiple  temporary
61%	modules. This test was first of all  designed to test for memory
62%	leaks.
63
64test_tmp_module(N) :-
65	length(L, N),
66	concurrent_maplist(tmp_queen_list, L),
67	garbage_collect_atoms.
68
69queens_file(File) :-
70	source_file(test_tmp_module, Here),
71	file_directory_name(Here, Dir),
72	atomic_list_concat([Dir, /, 'data/queens.pl'], File).
73
74tmp_queens(S) :-
75	N is random(1<<62), % was uuid(UUID), but that is a package
76	thread_self(Me),
77	thread_property(Me, id(Id)),
78	format(atom(UUID), 'tmp-~w-~d', [Id, N]),
79	queens_file(Queens),
80	in_temporary_module(
81	    UUID,
82	    setup_call_cleanup(
83		open(Queens, read, In),
84		load_files(UUID,
85			   [ module(UUID),
86			     stream(In),
87			     silent(true)
88			   ]),
89		close(In)),
90	    call(queens(8, S))).
91
92tmp_queen_list(L) :-
93	findnsols(10, S, tmp_queens(S), L), !.
94
95%!	test_op
96%
97%	Test current_op/3 on a disappearing temporary module.
98
99test_op :-
100    A is random(1000000),
101    atom_concat('tmp-', A, M),
102    thread_create(in_temporary_module(M, true, sleep(0.001)), Id),
103    get_time(Now),
104    repeat,
105       ignore(current_op(_, _, M:v)),
106       get_time(T),
107       T - Now > 0.002,
108       !,
109    thread_join(Id).
110