Top Banner
Integração Contínua Terra Ágil Nov/2010
53

Terra Ágil - Integração Contínua

Aug 11, 2015

Download

Technology

Diego Pereira
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Terra Ágil - Integração Contínua

Integração Contínua

Terra ÁgilNov/2010

Page 2: Terra Ágil - Integração Contínua

IC

– Integração Contínua

“Continuous Integration is a software development practice where members of a team integrate their work frequently, usually each person integrates at least daily - leading to multiple integrations per day. Each integration is verified by an automated build (including test) to detect integration errors as quickly as possible. Many teams find that this approach leads to significantly reduced integration problems and allows a team to develop cohesive software more rapidly.”

http://www.martinfowler.com/articles/continuousIntegration.html

Page 3: Terra Ágil - Integração Contínua

IC

– IC @ Atlas : • Testes: Unidade/Funcionais (Integração)

Page 4: Terra Ágil - Integração Contínua

IC

– Bottom Up! • Injeção de Dependência• Mocks• TDD• Integracao Continua / Hudson

– Conclusão

Page 5: Terra Ágil - Integração Contínua

IC

– Dependency Injection• Design Pattern• Reduzir acoplamento• Vários tipos (constructor injection, setter injection,

interface injection)

Page 6: Terra Ágil - Integração Contínua

IC

– Dependency Injection

class HardcodedWorkflow{

public void execute(){

// do LOTS of workflow stuff...

FileSenderService service = new FileSenderService(fileName, "This is a message");

service.send();

// do LOTS more workflow stuff...

}}

Page 7: Terra Ágil - Integração Contínua

IC

– Dependency Injection

class FileSenderService implements ISenderService{

private String message;private File file;

public FileSenderService(String fileName, String message){

this.parameter = message;this.file = File.open(fileName);

}

public void send(){

file.write(message);}

}

Page 8: Terra Ágil - Integração Contínua

IC

– Dependency Injection

class EmailSenderService implements ISenderService{

...

public void send(){

smtpLib.send(emailAddress, message);}

}

Page 9: Terra Ágil - Integração Contínua

IC

É só um “IF”

Page 10: Terra Ágil - Integração Contínua

IC

É só um “IF”

Page 11: Terra Ágil - Integração Contínua

IC

– Dependency Injection

class HardcodedWorkflow{

public void execute(int delivery){

// do LOTS of workflow stuff...

ISenderService service;if(delivery == MsgDelivery.FILE) {

service = new FileSenderService("This is a message");} else if(delivery == MsgDelivery.EMAIL) {

service = new EmailSenderService("This is a slightly different message");}

service.send();

// do LOTS more workflow stuff...

}}

Page 12: Terra Ágil - Integração Contínua

IC

2 meses depois ...

Page 13: Terra Ágil - Integração Contínua

IC

– Dependency Injection

...if(delivery == MsgDelivery.FILE) {

service = new FileSenderService("This is a message"); } else if(delivery == MsgDelivery.EMAIL) {

service = new EmailSenderService(userAddress, "This is a slightly different message"");} else if(delivery == MsgDelivery.ADMINISTRATOR_EMAIL) {

service = new EmailSenderService(adminAddress, "SUDO this is a message!!!");} else if(delivery == MsgDelivery.ABU_DHABI_BY_AIR_MAIL) {

service = new AbuDhabiAirMailSenderService("Hello, Mama");} else if(delivery == MsgDelivery.DEVNULL) {

service = new DevNullSenderService("Hello, is anybody in there??!?!"); // why do I care?!?!}...

Page 14: Terra Ágil - Integração Contínua

IC

2 Anos depois ...

Page 15: Terra Ágil - Integração Contínua

IC

Page 16: Terra Ágil - Integração Contínua

IC

– Dependency Injection To The Rescue!!!

class FlexibleWorkflow{

private ISenderService service;

public FlexibleWorkflow(ISenderService service) // << "LOOSE COUPLING"{

this.service = service;}

public void execute(){

// do LOTS of workflow stuff...

this.service.send();

// do LOTS more workflow stuff...

}}

Page 17: Terra Ágil - Integração Contínua

IC

– Dependency Injection To The Rescue!!!

class FileWorkflowController{

public void handleRequest(){

ISenderService service = new FileSenderService("This is a message."); // << "TIGHT COUPLING!!!"

FlexibleWorkflow workflow = new FlexibleWorkflow(service);

workflow.execute();}

}

Page 18: Terra Ágil - Integração Contínua

IC

– Dependency Injection To The Rescue!!!

class EmailWorkflowController{

public void handleRequest(){

ISenderService service = new EmailSenderService(getUserEmail(), "This is a slightly different message.");

FlexibleWorkflow workflow = new FlexibleWorkflow(service);

workflow.execute();}

}

Page 19: Terra Ágil - Integração Contínua

IC

– Dependency Injection• Ok, legal, mas o quê isso tem a ver com o pastel?!?!

Page 20: Terra Ágil - Integração Contínua

IC

– Testes com Mocks!!!

class FlexibleWorkflowTest extends TestCase{

// millions of other tests...

public void testMessageSend(){

ISenderService mockService = new MockSenderService();

FlexibleWorkflow workflow = new FlexibleWorkflow(mockService);

workflow.execute();

assertTrue(workflow.getResult(), true);

mockService.assertCalled("send");// millions of other assertions...

}}

Page 21: Terra Ágil - Integração Contínua

IC

– Mock Objects• Mocks aren’t Stubs!

http://martinfowler.com/articles/mocksArentStubs.html

Page 22: Terra Ágil - Integração Contínua

IC

– Stubs

ISenderService stubService = new StubService();

FlexibleWorkflow workflow = new FlexibleWorkflow(stubService);workflow.execute();

// nada mais a fazer, o stub nao provê nenhuma informacao

Page 23: Terra Ágil - Integração Contínua

IC

– Stubs

Page 24: Terra Ágil - Integração Contínua

IC

– Mock Objects• Estilo diferente de testes

Page 25: Terra Ágil - Integração Contínua

IC

– Mock Objects• Inspecionar comportamento

class FlexibleWorkflowTest extends TestCase{

public void testMessageSend(){

ISenderService mockService = new MockSenderService();

FlexibleWorkflow workflow = new FlexibleWorkflow(mockService);

workflow.execute();

assertTrue(workflow.getResult(), true);

mockService.assertCalled("send");// millions of other assertions...

}}

Page 26: Terra Ágil - Integração Contínua

IC

– Mock Objects• Simulando condições de erro

public void testMessageSendError(){

ISenderService mockService = new MockSenderService();

// mock lanca "SmtpTimeoutException" quando// metodo "send" e chamadomockService.setSideEffect("send",

new SideEffect() { void run () { throw new SmtpTimeoutException(); } });

FlexibleWorkflow workflow = new FlexibleWorkflow(mockService);

try {workflow.execute();

} catch(Exception e) {assertFail();

}}

interface SideEffect {void run();

}

Page 27: Terra Ágil - Integração Contínua

IC

– Mock Objects• Mocks sao agentes do codigo de teste

Page 28: Terra Ágil - Integração Contínua

IC

– TDD

“You have probably heard a few talks or read a few articles where test driven development is depicted as a magic unicorn that watches over your software and makes everybody happy.”

Page 29: Terra Ágil - Integração Contínua

IC

– TDD

“ Well, about 18.000 lines of ‘magic unicorn’ code later, I'd like to put things into perspective.”

Page 30: Terra Ágil - Integração Contínua

IC

– TDD

“The truth is, test driven development is a huge pain in the ass.”

Page 31: Terra Ágil - Integração Contínua

IC

– TDD

“But do you know what sucks more? The liability that comes without those tests.”

http://debuggable.com/posts/test-driven-development-at-transloadit:4cc892a7-b9fc-4d65-bb0c-1b27cbdd56cb

Page 32: Terra Ágil - Integração Contínua

IC

– TDD• Ok, “Test First” ...• ... mas por onde eu começo?!?!

Page 33: Terra Ágil - Integração Contínua

IC

– TDD• Ok, “Test First” ...• ... mas por onde eu começo?!?!

Page 34: Terra Ágil - Integração Contínua

IC

– TDD• Implementar mínimo necessário para o teste FALHAR.

Page 35: Terra Ágil - Integração Contínua

IC

– TDD• Implementar mínimo necessário para o teste FALHAR.

class Math{

public int sum(int a, int b) {

throw new NotImplementedException();}

}

class MathTest extends TestCase{

public void testSum(){

Math m = new Math();assertEquals(m.sum(1,2), 3);

}}

Page 36: Terra Ágil - Integração Contínua

IC

– TDD• Vantagens

Page 37: Terra Ágil - Integração Contínua

IC

– TDD• Vantagens

– Código é testável “por natureza”.

Page 38: Terra Ágil - Integração Contínua

IC

– TDD• Vantagens

– Código é testável “por natureza”.– Ajuda a usar Mocks e Injeção de Dependências.

Page 39: Terra Ágil - Integração Contínua

IC

– TDD• Vantagens

– Código é testável “por natureza”.– Ajuda a usar Mocks e Injeção de Dependências.– Cobertura de Código.

Page 40: Terra Ágil - Integração Contínua

IC

– TDD• Vantagens

– Código é testável “por natureza”.– Ajuda a usar Mocks e Injeção de Dependências.– Cobertura de Código.– Design gera interfaces (de código) mais “amigáveis”.

Page 41: Terra Ágil - Integração Contínua

IC

– TDD• Vantagens

– Código é testável “por natureza”.– Ajuda a usar Mocks e Injeção de Dependências.– Cobertura de Código.– Design gera interfaces (de código) mais “amigáveis”.– TDD não tem “ciúminho”.

Page 42: Terra Ágil - Integração Contínua

IC

– TDD• Desvantagens

Page 43: Terra Ágil - Integração Contínua

IC

– TDD• Desvantagens

– Na 1a. vez, prepare-se para errar.

Page 44: Terra Ágil - Integração Contínua

IC

– TDD• Desvantagens

– Na 1a. vez, prepare-se para errar. Muito.

Page 45: Terra Ágil - Integração Contínua

IC

– TDD• Desvantagens

– Na 1a. vez, prepare-se para errar. Muito.» Mas aprender, mais ainda!

Page 46: Terra Ágil - Integração Contínua

IC

– TDD• Desvantagens

– Na 1a. vez, prepare-se para errar. Muito.» Mas aprender, mais ainda!

– Nada é de graça (No Unicorns)

Page 47: Terra Ágil - Integração Contínua

IC

– TDD• Desvantagens

– Na 1a. vez, prepare-se para errar. Muito.» Mas aprender, mais ainda!

– Nada é de graça (No Unicorns)– Código legado

Page 48: Terra Ágil - Integração Contínua

IC

– TDD• Desvantagens

– Na 1a. vez, prepare-se para errar. Muito.» Mas aprender, mais ainda!

– Nada é de graça (No Unicorns)– Código legado– É um “pé no saco”

Page 49: Terra Ágil - Integração Contínua

IC

– Build “On-Commit”– Build Noturno– Testes– Artefatos– RadiadoresDe Informação

Page 50: Terra Ágil - Integração Contínua

IC

• Hudson e o “Gremlin”

[email protected]

Page 51: Terra Ágil - Integração Contínua

IC

• Hudson e o “Gremlin”– Dificuldades

• Makefiles• Ambiente

Page 52: Terra Ágil - Integração Contínua

Conclusão

• There’s no Free Lunch ...• ... and no Silver Bullet!• Baby Steps!• Purismo não leva a lugar nenhum• Ferramentas adequadas