Skip to content

Commit

Permalink
Fix compiled scripts demo from README
Browse files Browse the repository at this point in the history
Compiled functions always show up with no metadata, and you can only
guess what the types are from the bean definition. Probably we should
add more information to the bean definition if we have it when we
compile the function.

There is still a problem if user defines functions that are not of
Flux<String> (but that has always been the case).
  • Loading branch information
dsyer committed May 25, 2017
1 parent 41b1a65 commit c685866
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 7 deletions.
6 changes: 3 additions & 3 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ Also, start a RabbitMQ server locally (e.g. execute `rabbitmq-server`).

----
./web.sh -f uppercase -p 9000
curl -H "Content-Type: text/plain" -H "Accept: text/plain" :9000/uppercase -d foo
curl -H "Content-Type: text/plain" -H "Accept: text/plain" localhost:9000/uppercase -d foo
----

=== Register a Supplier:
Expand All @@ -150,7 +150,7 @@ curl -H "Content-Type: text/plain" -H "Accept: text/plain" :9000/uppercase -d fo

----
./web.sh -s words -p 9001
curl -H "Accept: application/json" :9001/words
curl -H "Accept: application/json" localhost:9001/words
----

=== Register a Consumer:
Expand All @@ -163,7 +163,7 @@ curl -H "Accept: application/json" :9001/words

----
./web.sh -c print -p 9002
curl -X POST -H "Content-Type: text/plain" -d foo :9002/print
curl -X POST -H "Content-Type: text/plain" -d foo localhost:9002/print
----

=== Run Stream Processing Microservices:
Expand Down
2 changes: 1 addition & 1 deletion scripts/registerConsumer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ while getopts ":n:f:" opt; do
esac
done

curl -X POST -H "Content-Type: text/plain" -d $FUNC :8080/consumer/$NAME
curl -X POST -H "Content-Type: text/plain" -d $FUNC localhost:8080/consumer/$NAME
2 changes: 1 addition & 1 deletion scripts/registerFunction.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ while getopts ":n:f:i:o:" opt; do
esac
done

curl -X POST -H "Content-Type: text/plain" -d $FUNC ":8080/function/$NAME?inputType=$INTYPE&outputType=$OUTTYPE"
curl -X POST -H "Content-Type: text/plain" -d $FUNC "localhost:8080/function/$NAME?inputType=$INTYPE&outputType=$OUTTYPE"

2 changes: 1 addition & 1 deletion scripts/registerSupplier.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ while getopts ":n:f:t:" opt; do
esac
done

curl -X POST -H "Content-Type: text/plain" -d $FUNC :8080/supplier/$NAME?type=$TYPE
curl -X POST -H "Content-Type: text/plain" -d $FUNC localhost:8080/supplier/$NAME?type=$TYPE
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.function.Supplier;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.BeanDefinition;
Expand Down Expand Up @@ -422,7 +423,10 @@ else if (source instanceof MethodMetadataReadingVisitor) {
param = resolvable.getGeneric(index).getGeneric(0).getType();
}
else {
// TODO: compiled functions only work as String -> String
// TODO: compiled functions (only work as String -> String)
if (paramType.isWrapper()) {
return Flux.class;
}
return String.class;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright 2012-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.cloud.function.web;

import java.net.URI;
import java.util.function.Supplier;

import org.junit.Test;
import org.junit.runner.RunWith;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;

import static org.assertj.core.api.Assertions.assertThat;

import reactor.core.publisher.Flux;

/**
* @author Dave Syer
*
*/
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class SingletonTests {

@LocalServerPort
private int port;
@Autowired
private TestRestTemplate rest;

@Test
public void words() throws Exception {
ResponseEntity<String> result = rest.exchange(
RequestEntity.get(new URI("/words")).build(), String.class);
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(result.getBody()).isEqualTo("[\"foo\",\"bar\"]");
}

@EnableAutoConfiguration
@org.springframework.boot.test.context.TestConfiguration
protected static class TestConfiguration {
@Bean
public static BeanDefinitionRegistryPostProcessor processor() {
return new BeanDefinitionRegistryPostProcessor() {

@Override
public void postProcessBeanFactory(
ConfigurableListableBeanFactory beanFactory)
throws BeansException {
}

@Override
public void postProcessBeanDefinitionRegistry(
BeanDefinitionRegistry registry) throws BeansException {
// Simulates what happens whem you add a compiled function
RootBeanDefinition beanDefinition = new RootBeanDefinition(MySupplier.class);
registry.registerBeanDefinition("words", beanDefinition);
}
};
}
}

static class MySupplier implements Supplier<Flux<String>> {
@Override
public Flux<String> get() {
return Flux.just("foo", "bar");
}
}
}

0 comments on commit c685866

Please sign in to comment.