<commit_before>package io.muoncore.inmem.transport

import com.google.common.eventbus.EventBus
import io.muoncore.Discovery
import io.muoncore.channel.support.Scheduler
import io.muoncore.codec.Codecs
import io.muoncore.config.AutoConfiguration
import io.muoncore.memory.transport.InMemClientChannelConnection
import io.muoncore.memory.transport.InMemTransport
import io.muoncore.memory.transport.OpenChannelEvent
import io.muoncore.message.MuonInboundMessage
import io.muoncore.protocol.ServerStacks
import io.muoncore.transport.TransportEvents
import io.muoncore.transport.TransportFailureSpec
import spock.lang.Specification
import spock.util.concurrent.PollingConditions

class InMemTransportSpec extends Specification {

  Codecs codecs = Mock(Codecs) {
    encode(_, _) >> new Codecs.EncodingResult(new byte[0], "application/json")
  }

  def "returns client channel connection on demand."() {
    def eventbus = new EventBus()
    def serverStacks = Mock(ServerStacks)

    def transport = new InMemTransport(new AutoConfiguration(serviceName: "tombola"), eventbus)
    transport.start(Mock(Discovery), serverStacks, codecs, new Scheduler())

    when:
    def ret = transport.openClientChannel("tombola", "simple")

    then:
    ret instanceof InMemClientChannelConnection
  }

  def "transport listens on event bus for OpenChannelEvents. opens channels in response"() {
    def eventbus = new EventBus()
    def serverStacks = Mock(ServerStacks)
    def clientConnection = Mock(InMemClientChannelConnection)

    def transport = new InMemTransport(new AutoConfiguration(serviceName: "tombola"), eventbus)
    transport.start(Mock(Discovery), serverStacks, codecs, new Scheduler())

    when:
    eventbus.post(new OpenChannelEvent(
      "tombola", "simple", clientConnection))

    then:
    1 * serverStacks.openServerChannel("simple")
  }

  def "when failurs is triggered, channel connections are immediately closed with a transport failure error"() {
    def eventbus = new EventBus()
    def serverStacks = Mock(ServerStacks)

    def transport = new InMemTransport(new AutoConfiguration(serviceName: "tombola"), eventbus)
    transport.start(Mock(Discovery), serverStacks, codecs, new Scheduler())
    transport.triggerFailure()

    when:
    def ret = transport.openClientChannel("tombola", "simple")

    MuonInboundMessage fail

    ret.receive {
      fail = it
    }
<commit_msg>Update test, will fail on first message send as per normal channel behaviour.<commit_after>package io.muoncore.inmem.transport

import com.google.common.eventbus.EventBus
import io.muoncore.Discovery
import io.muoncore.channel.support.Scheduler
import io.muoncore.codec.Codecs
import io.muoncore.config.AutoConfiguration
import io.muoncore.memory.transport.InMemClientChannelConnection
import io.muoncore.memory.transport.InMemTransport
import io.muoncore.memory.transport.OpenChannelEvent
import io.muoncore.message.MuonInboundMessage
import io.muoncore.message.MuonMessageBuilder
import io.muoncore.protocol.ServerStacks
import io.muoncore.transport.TransportEvents
import io.muoncore.transport.TransportFailureSpec
import spock.lang.Specification
import spock.util.concurrent.PollingConditions

class InMemTransportSpec extends Specification {

  Codecs codecs = Mock(Codecs) {
    encode(_, _) >> new Codecs.EncodingResult(new byte[0], "application/json")
  }

  def "returns client channel connection on demand."() {
    def eventbus = new EventBus()
    def serverStacks = Mock(ServerStacks)

    def transport = new InMemTransport(new AutoConfiguration(serviceName: "tombola"), eventbus)
    transport.start(Mock(Discovery), serverStacks, codecs, new Scheduler())

    when:
    def ret = transport.openClientChannel("tombola", "simple")

    then:
    ret instanceof InMemClientChannelConnection
  }

  def "transport listens on event bus for OpenChannelEvents. opens channels in response"() {
    def eventbus = new EventBus()
    def serverStacks = Mock(ServerStacks)
    def clientConnection = Mock(InMemClientChannelConnection)

    def transport = new InMemTransport(new AutoConfiguration(serviceName: "tombola"), eventbus)
    transport.start(Mock(Discovery), serverStacks, codecs, new Scheduler())

    when:
    eventbus.post(new OpenChannelEvent(
      "tombola", "simple", clientConnection))

    then:
    1 * serverStacks.openServerChannel("simple")
  }

  def "when failurs is triggered, channel connections are immediately closed with a transport failure error"() {
    def eventbus = new EventBus()
    def serverStacks = Mock(ServerStacks)

    def transport = new InMemTransport(new AutoConfiguration(serviceName: "tombola"), eventbus)
    transport.start(Mock(Discovery), serverStacks, codecs, new Scheduler())
    transport.triggerFailure()

    when:
    def ret = transport.openClientChannel("tombola", "simple")

    MuonInboundMessage fail

    ret.receive {
      fail = it
    }

    ret.send(MuonMessageBuilder.fromService("me").build())

{'fraction_non_alphanumeric': 0.09006835544833132, 'fraction_numerical': 0.0008041817450743868, 'mean_word_length': 4.90855106888361, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4943319', 'n_tokens_mistral': 1433, 'n_tokens_neox': 1399, 'n_words': 345}