forked from AdaCore/aws
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
U503-007 * src/core/aws-utils.adb (Shared_Random): Object to protect random number generator from multi task concurrency. (Random_Reset): New routine to initate predictable random sequence. * regtests/0336_random_task_safe/test.adb, regtests/0336_random_task_safe/test.gpr, regtests/0336_random_task_safe/test.py: Test for random generation concurrency.
- Loading branch information
Showing
6 changed files
with
184 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
with Ada.Containers.Hashed_Sets; | ||
|
||
with Ada.Exceptions; use Ada.Exceptions; | ||
with Ada.Text_IO; use Ada.Text_IO; | ||
with AWS.Utils; use AWS.Utils; | ||
|
||
procedure Test is | ||
|
||
type Test_Random_Mode is (None, Save, Lookup); | ||
|
||
package Random_Sets is new Ada.Containers.Hashed_Sets | ||
(Random_Integer, Ada.Containers.Hash_Type'Mod, "="); | ||
|
||
Set : Random_Sets.Set; | ||
Locker : RW_Semaphore (1); | ||
Wrong_Data_Set : exception; | ||
|
||
procedure Test_Random_Sequence | ||
(Count : Positive; Mode : Test_Random_Mode); | ||
|
||
function Test_Random_Sequence | ||
(Initialor : Integer; Count : Positive) return String; | ||
|
||
task Secondary is | ||
entry Start (Count : Positive; Mode : Test_Random_Mode); | ||
entry Done (Quit : Boolean); | ||
end Secondary; | ||
|
||
-------------------------- | ||
-- Test_Random_Sequence -- | ||
-------------------------- | ||
|
||
function Test_Random_Sequence | ||
(Initialor : Integer; Count : Positive) return String is | ||
begin | ||
Random_Reset (Initialor); | ||
Test_Random_Sequence (Count, None); | ||
return Random_String (77); | ||
end Test_Random_Sequence; | ||
|
||
procedure Test_Random_Sequence | ||
(Count : Positive; Mode : Test_Random_Mode) | ||
is | ||
R : Random_Integer; | ||
begin | ||
for J in 1 .. Count loop | ||
R := Random; | ||
|
||
case Mode is | ||
when None => | ||
null; | ||
|
||
when Save => | ||
Locker.Write; | ||
Set.Include (R); | ||
Locker.Release_Write; | ||
|
||
when Lookup => | ||
if not Set.Contains (R) then | ||
raise Wrong_Data_Set with | ||
"Expected random element" & R'Img & " absent at" & J'Img; | ||
end if; | ||
end case; | ||
end loop; | ||
|
||
end Test_Random_Sequence; | ||
|
||
--------------- | ||
-- Secondary -- | ||
--------------- | ||
|
||
task body Secondary is | ||
Count : Positive; | ||
Mode : Test_Random_Mode; | ||
Quit : Boolean; | ||
begin | ||
loop | ||
accept Start (Count : Positive; Mode : Test_Random_Mode) do | ||
Secondary.Count := Count; | ||
Secondary.Mode := Mode; | ||
end Start; | ||
|
||
Test_Random_Sequence (Count, Mode); | ||
|
||
accept Done (Quit : Boolean) do | ||
Secondary.Quit := Quit; | ||
end Done; | ||
|
||
exit when Quit; | ||
end loop; | ||
|
||
exception | ||
when E : others => | ||
Put_Line ("Secondary task: " & Exception_Message (E)); | ||
end Secondary; | ||
|
||
begin | ||
if Test_Random_Sequence (321, 256) /= Test_Random_Sequence (321, 256) then | ||
Put_Line ("Random_Reset with same initialor error"); | ||
end if; | ||
|
||
for Mode in Save .. Lookup loop | ||
Random_Reset (112344); | ||
Test_Random_Sequence (4096, Mode); | ||
end loop; | ||
|
||
Set.Clear; | ||
|
||
for Mode in Save .. Lookup loop | ||
Random_Reset (44552211); | ||
Secondary.Start (8196, Mode); | ||
Test_Random_Sequence (8196, Mode); | ||
Secondary.Done (Mode = Lookup); | ||
end loop; | ||
|
||
exception | ||
when E : others => | ||
Put_Line ("Main task: " & Exception_Message (E)); | ||
end Test; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
with "aws.gpr"; | ||
|
||
project Test is | ||
for Main use ("test.adb"); | ||
end Test; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from test_support import * | ||
|
||
build_and_run('test'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters