Skip to content

Commit 7bbac33

Browse files
committed
#25 fix test error after using async method
1 parent e873e00 commit 7bbac33

File tree

9 files changed

+31
-13
lines changed

9 files changed

+31
-13
lines changed

wechaty/src/main/scala/wechaty/plugins/DingDong.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package wechaty.plugins
22

3+
import com.typesafe.scalalogging.LazyLogging
34
import wechaty.user.Message
45
import wechaty.{Wechaty, WechatyPlugin}
56

@@ -37,7 +38,7 @@ case class DingDongConfig(
3738
*/
3839
var dingReg:String="^#ding$",
3940
)
40-
class DingDongPlugin(config:DingDongConfig,/*only for test*/isWait:Boolean=false) extends WechatyPlugin{
41+
class DingDongPlugin(config:DingDongConfig,/*only for test*/isWait:Boolean=false) extends WechatyPlugin with LazyLogging{
4142
private val DONG="dong"
4243
private val DING_REGEXP=("("+config.dingReg+")").r
4344
private def isMatch(message: Message): Boolean ={

wechaty/src/main/scala/wechaty/plugins/FriendshipAcceptor.scala

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@ import wechaty.puppet.schemas.Puppet._
88
import wechaty.user.Contact
99
import wechaty.{Wechaty, WechatyPlugin}
1010

11+
import scala.concurrent.duration._
12+
import scala.concurrent.Await
13+
1114
/**
1215
*
1316
* @author <a href="mailto:[email protected]">Jun Tsai</a>
1417
* @since 2020-06-19
1518
*/
1619
case class FriendshipAcceptorConfig( var greeting:String = "we are friends now!",var waitSeconds:Int=0, var keywordOpt:Option[String]=None)
17-
class FriendshipAcceptor(config:FriendshipAcceptorConfig) extends WechatyPlugin with StrictLogging{
20+
class FriendshipAcceptor(config:FriendshipAcceptorConfig,/*for test*/isWait:Boolean=false) extends WechatyPlugin with StrictLogging{
1821
private def isMatchKeyword(str: String): Boolean ={
1922
logger.debug("keyword:{} hello:{} ",config.keywordOpt,str)
2023
config.keywordOpt match{
@@ -29,7 +32,9 @@ class FriendshipAcceptor(config:FriendshipAcceptorConfig) extends WechatyPlugin
2932
}
3033
}
3134
private def doGreeting(contact: Contact): Unit ={
32-
contact.say(config.greeting)
35+
val future = contact.say(config.greeting)
36+
if(isWait)
37+
Await.result(future,10 seconds)
3338
}
3439
override def install(wechaty: Wechaty): Unit = {
3540
wechaty.onFriendAdd(friendship => {

wechaty/src/main/scala/wechaty/plugins/RoomConnector.scala

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import wechaty.plugins.RoomConnector._
55
import wechaty.user.{Message, Room}
66
import wechaty.{Wechaty, WechatyPlugin}
77

8+
import scala.concurrent.duration._
9+
import scala.concurrent.Await
10+
811
/**
912
*
1013
* @author <a href="mailto:[email protected]">Jun Tsai</a>
@@ -22,7 +25,7 @@ object RoomConnector {
2225
type RoomMessageMapper = (/* from room */Room,Message,/* to room */Room) => Option[Message]
2326
val DEFAULT_MESSAGE_SENDER:RoomMessageMapper=(_,msg,_) => Some(msg)
2427
}
25-
class RoomConnector(config: RoomConnectorConfig) extends WechatyPlugin with LazyLogging {
28+
class RoomConnector(config: RoomConnectorConfig,/*only for test*/isWait:Boolean=false) extends WechatyPlugin with LazyLogging {
2629
override def install(wechaty: Wechaty): Unit = {
2730
implicit val resolver: Wechaty = wechaty
2831
wechaty.onOnceMessage(message => {
@@ -35,7 +38,9 @@ class RoomConnector(config: RoomConnectorConfig) extends WechatyPlugin with Lazy
3538
toRooms foreach { toRoom =>
3639
config.mapper(fromRoom, roomMessage, toRoom) match {
3740
case Some(msg) =>
38-
msg.forward(toRoom)
41+
val future = msg.forward(toRoom)
42+
if(isWait)
43+
Await.result(future,10 seconds)
3944
case _ => //filtered,so don't forward message
4045
}
4146
}

wechaty/src/main/scala/wechaty/plugins/RoomJoinHello.scala

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@ package wechaty.plugins
33
import com.typesafe.scalalogging.LazyLogging
44
import wechaty.{Wechaty, WechatyPlugin}
55

6+
import scala.concurrent.duration._
7+
import scala.concurrent.Await
8+
69
/**
710
*
811
* @author <a href="mailto:[email protected]">Jun Tsai</a>
912
* @since 2020-06-23
1013
*/
1114
case class RoomJoinHelloConfig( var rooms:Array[String]=Array() ,hello:String="welcome")
12-
class RoomJoinHello(config:RoomJoinHelloConfig) extends WechatyPlugin with LazyLogging{
15+
class RoomJoinHello(config:RoomJoinHelloConfig,/*only for test*/isWait:Boolean=false) extends WechatyPlugin with LazyLogging{
1316
override def install(wechaty: Wechaty): Unit = {
1417
implicit val resolver:Wechaty = wechaty
1518
wechaty.onOnceMessage(message=>{
@@ -20,7 +23,11 @@ class RoomJoinHello(config:RoomJoinHelloConfig) extends WechatyPlugin with LazyL
2023
PluginHelper.executeWithNotThrow("RoomJoinHello") {
2124
if (list == null || list.isEmpty) {
2225
logger.warn("invitee list is empty")
23-
} else room.say(config.hello, list)
26+
} else {
27+
val future = room.say(config.hello, list)
28+
if(isWait)
29+
Await.result(future,10 seconds)
30+
}
2431
}
2532
}
2633
})

wechaty/src/main/scala/wechaty/user/Message.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ class Message(messageId:String)(implicit resolver: PuppetResolver) extends LazyL
180180
case _ => false
181181
}
182182
}
183-
def forward (to: Conversation): Unit = {
183+
def forward (to: Conversation): Future[String]= {
184184
resolver.puppet.messageForward(to.id, this.messageId)
185185
}
186186
/**

wechaty/src/test/scala/wechaty/plugins/DingDongTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class DingDongTest extends TestBase{
6262
def test_plugin: Unit ={
6363
val config = new DingDongConfig
6464
config.room = true
65-
instance.use(new DingDongPlugin(config))
65+
instance.use(new DingDongPlugin(config,isWait = true))
6666
mockRoomMessage("#ding")
6767
mockMessageSendText()
6868

wechaty/src/test/scala/wechaty/plugins/FriendshipAcceptorTest.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class FriendshipAcceptorTest extends TestBase{
1515
@Test
1616
def test_add: Unit ={
1717
val config = FriendshipAcceptorConfig()
18-
instance.use(new FriendshipAcceptor(config))
18+
instance.use(new FriendshipAcceptor(config,isWait = true))
1919

2020
mockFriendshipAdd()
2121
emitFriendshipAddPayloadEvent()
@@ -46,7 +46,7 @@ class FriendshipAcceptorTest extends TestBase{
4646
@Test
4747
def testGreetings: Unit ={
4848
val config = FriendshipAcceptorConfig()
49-
instance.use(new FriendshipAcceptor(config))
49+
instance.use(new FriendshipAcceptor(config,isWait = true))
5050
config.greeting="你好"
5151

5252
mockFriendshipAdd(payloadType = FriendshipType.FRIENDSHIP_TYPE_CONFIRM)

wechaty/src/test/scala/wechaty/plugins/RoomConnectorTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class RoomConnectorTest extends TestBase{
1515
def test_connector: Unit ={
1616
val connectorConfig = RoomConnectorConfig(Array("from"),Array("to"))
1717
connectorConfig.blacklist = _ => true
18-
instance.use(new RoomConnector(connectorConfig))
18+
instance.use(new RoomConnector(connectorConfig,isWait = true))
1919

2020
mockRoomMessage(roomId = "from")
2121
mockMessageSendText()

wechaty/src/test/scala/wechaty/plugins/RoomJoinHelloTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import wechaty.puppet.schemas.Puppet.PuppetEventName
1515
class RoomJoinHelloTest extends TestBase{
1616
@Test
1717
def test_join: Unit ={
18-
val roomJoinHello = new RoomJoinHello(RoomJoinHelloConfig(rooms = Array("roomId"),hello = "hello world!"))
18+
val roomJoinHello = new RoomJoinHello(RoomJoinHelloConfig(rooms = Array("roomId"),hello = "hello world!"),isWait=true)
1919
roomJoinHello.install(instance)
2020

2121
mockRoomMessage(roomId = "from")

0 commit comments

Comments
 (0)