﻿1
00:00:00,210 --> 00:00:04,799
you

2
00:00:04,799 --> 00:00:09,400
welcome to section 6 testing actors in

3
00:00:09,400 --> 00:00:12,250
the previous section working with akka

4
00:00:12,250 --> 00:00:14,378
cluster we were introduced to the occur

5
00:00:14,378 --> 00:00:16,600
Custer and touched upon topics such as

6
00:00:16,600 --> 00:00:18,879
remote actor rooting on the cluster

7
00:00:18,879 --> 00:00:22,329
cluster sharding and cluster singleton

8
00:00:22,329 --> 00:00:25,239
in this section we will shift our focus

9
00:00:25,239 --> 00:00:28,539
to testing different aspects of a cur we

10
00:00:28,539 --> 00:00:30,460
will test our actors parent-child

11
00:00:30,460 --> 00:00:33,840
relationships and finite state machines

12
00:00:33,840 --> 00:00:36,549
finally we will take a look at multimode

13
00:00:36,549 --> 00:00:40,319
tests let's start with the first video

14
00:00:40,319 --> 00:00:44,619
how to test an actor in this video we

15
00:00:44,619 --> 00:00:46,238
will take a look at the different styles

16
00:00:46,238 --> 00:00:48,759
to test the actor and we will start with

17
00:00:48,759 --> 00:00:51,579
coding acha comes with a dedicated

18
00:00:51,579 --> 00:00:54,069
module for supporting tests the akka

19
00:00:54,069 --> 00:00:57,429
test kit let's add the aqha test kit

20
00:00:57,429 --> 00:00:59,198
module and scarlett s to our

21
00:00:59,198 --> 00:01:03,189
dependencies here is the counter actor

22
00:01:03,189 --> 00:01:05,500
which handles the messages increment

23
00:01:05,500 --> 00:01:08,590
decrement and get count and has a count

24
00:01:08,590 --> 00:01:11,590
variable we want to implement our tests

25
00:01:11,590 --> 00:01:14,680
for this actor let's create a counter

26
00:01:14,680 --> 00:01:16,810
spec class that extends the test kit

27
00:01:16,810 --> 00:01:19,269
class which gives us the basic tools we

28
00:01:19,269 --> 00:01:22,420
need to work with actors this includes

29
00:01:22,420 --> 00:01:24,849
access to the actor system as well as

30
00:01:24,849 --> 00:01:26,200
helper methods for dealing with

31
00:01:26,200 --> 00:01:29,140
responses from actors under test we will

32
00:01:29,140 --> 00:01:30,909
be using Scala test in this video

33
00:01:30,909 --> 00:01:33,578
however it's possible to test occur

34
00:01:33,578 --> 00:01:36,280
using different testing frameworks the

35
00:01:36,280 --> 00:01:38,259
test kit provided by acha is test

36
00:01:38,259 --> 00:01:40,989
framework independent in case of Scala

37
00:01:40,989 --> 00:01:43,420
test we need to extend the flat spec

38
00:01:43,420 --> 00:01:45,310
like trait to get the general test

39
00:01:45,310 --> 00:01:48,159
syntax available before and after all to

40
00:01:48,159 --> 00:01:49,450
be able to hook into the Suites

41
00:01:49,450 --> 00:01:51,879
lifecycle events and we'll use the must

42
00:01:51,879 --> 00:01:54,159
matches to actually write our assertions

43
00:01:54,159 --> 00:01:57,149
using the X must do something style

44
00:01:57,149 --> 00:02:00,069
first let's shut down the actor system

45
00:02:00,069 --> 00:02:02,769
after all the tests finish for this

46
00:02:02,769 --> 00:02:06,069
override the after all method we will

47
00:02:06,069 --> 00:02:08,080
use the shutdown actor system method

48
00:02:08,080 --> 00:02:10,090
from test kit as our actor system was

49
00:02:10,090 --> 00:02:12,990
created by the test kit called system

50
00:02:12,990 --> 00:02:16,750
now let's add our test unit

51
00:02:16,750 --> 00:02:19,479
first let's define our sender actor that

52
00:02:19,479 --> 00:02:21,689
will send messages for our counter actor

53
00:02:21,689 --> 00:02:25,000
we will use test probes which allows us

54
00:02:25,000 --> 00:02:26,919
to send messages and expect response

55
00:02:26,919 --> 00:02:30,580
messages next let's define running an

56
00:02:30,580 --> 00:02:33,009
instance from our actor send an

57
00:02:33,009 --> 00:02:36,280
increment message and then get the count

58
00:02:36,280 --> 00:02:39,849
message now our sender should receive

59
00:02:39,849 --> 00:02:42,060
the response of the getcount message

60
00:02:42,060 --> 00:02:44,650
test probes provide some methods that

61
00:02:44,650 --> 00:02:47,889
help us to expect the messages let's use

62
00:02:47,889 --> 00:02:50,259
expect message type which expects a

63
00:02:50,259 --> 00:02:53,500
message of a specific type our response

64
00:02:53,500 --> 00:02:56,650
should be int if the sender receives a

65
00:02:56,650 --> 00:02:58,360
message with another type this

66
00:02:58,360 --> 00:03:01,840
expectation will fail finally let's

67
00:03:01,840 --> 00:03:04,209
assert on the value of the state the

68
00:03:04,209 --> 00:03:12,740
state must be equal to 1 let's run

69
00:03:12,740 --> 00:03:17,150
great it passes let's implement a test

70
00:03:17,150 --> 00:03:20,449
unit for increment messages only we do

71
00:03:20,449 --> 00:03:22,159
not want to set up a test probe each

72
00:03:22,159 --> 00:03:24,560
time in the test we can instead use the

73
00:03:24,560 --> 00:03:26,689
implicit senders trait which makes the

74
00:03:26,689 --> 00:03:28,430
test get able to receive messages

75
00:03:28,430 --> 00:03:31,819
directly the test kit contains the test

76
00:03:31,819 --> 00:03:34,490
actor which contains expectation methods

77
00:03:34,490 --> 00:03:36,979
such as test probe when we mix with

78
00:03:36,979 --> 00:03:39,770
implicit sender this test actor uses as

79
00:03:39,770 --> 00:03:43,129
a sender for any message in tests so

80
00:03:43,129 --> 00:03:44,840
let's define a running instance of a

81
00:03:44,840 --> 00:03:46,500
counter

82
00:03:46,500 --> 00:03:49,370
and send an increment message for it

83
00:03:49,370 --> 00:03:51,689
when the counter actor receives an

84
00:03:51,689 --> 00:03:53,699
increment message it just changes the

85
00:03:53,699 --> 00:03:55,860
count and doesn't send a response to the

86
00:03:55,860 --> 00:03:59,219
sender so let's expect that we didn't

87
00:03:59,219 --> 00:04:01,199
receive messages during that one second

88
00:04:01,199 --> 00:04:03,509
slot here we are using the implicit

89
00:04:03,509 --> 00:04:08,930
sender let's run great it passed as well

90
00:04:08,930 --> 00:04:11,610
now let's add a test unit for the

91
00:04:11,610 --> 00:04:13,409
decrement message like the previous one

92
00:04:13,409 --> 00:04:15,629
but we will send a decrement message and

93
00:04:15,629 --> 00:04:20,839
also expect no message let's run

94
00:04:20,839 --> 00:04:25,439
great now let's add a test forget count

95
00:04:25,439 --> 00:04:27,750
but we will use an implicit sender and

96
00:04:27,750 --> 00:04:30,180
send the getcount message only to make

97
00:04:30,180 --> 00:04:32,009
sure our actor starts with zero on the

98
00:04:32,009 --> 00:04:36,029
count variable so let's add an instance

99
00:04:36,029 --> 00:04:38,220
of the counter send the get count

100
00:04:38,220 --> 00:04:40,620
message and expect we will receive the

101
00:04:40,620 --> 00:04:46,639
message containing zero let's run

102
00:04:46,639 --> 00:04:50,100
excellent now let's add another test

103
00:04:50,100 --> 00:04:51,779
where we will send the increment

104
00:04:51,779 --> 00:04:55,740
decrement and get count messages let's

105
00:04:55,740 --> 00:04:57,899
create an instance of the counter then

106
00:04:57,899 --> 00:05:00,269
send to increment messages send a

107
00:05:00,269 --> 00:05:02,639
decrement message and also send another

108
00:05:02,639 --> 00:05:06,810
increment message now the counter actor

109
00:05:06,810 --> 00:05:10,139
should contain account that equals 2 so

110
00:05:10,139 --> 00:05:12,329
let's send a get count message and

111
00:05:12,329 --> 00:05:13,980
expect to receive a message that

112
00:05:13,980 --> 00:05:19,499
contains two let's run

113
00:05:19,499 --> 00:05:23,408
excellent all tests passed at the end of

114
00:05:23,408 --> 00:05:25,749
this video we implement a different unit

115
00:05:25,749 --> 00:05:28,509
test for each counter actor we use the

116
00:05:28,509 --> 00:05:31,329
recommended style from Acker you can

117
00:05:31,329 --> 00:05:33,189
check the test documentation from here

118
00:05:33,189 --> 00:05:34,809
and you'll find another style for

119
00:05:34,809 --> 00:05:36,848
testing called synchronous testing which

120
00:05:36,848 --> 00:05:39,009
uses test active ref but it's

121
00:05:39,009 --> 00:05:40,899
potentially exposed for race conditions

122
00:05:40,899 --> 00:05:42,439
and should not be used


