$30 off During Our Annual Pro Sale. View Details »

net/http/httptest.Server のアプローチをテスト戦略に活用する / Go...

net/http/httptest.Server のアプローチをテスト戦略に活用する / Go Conference 2023

Ken’ichiro Oyama

June 02, 2023
Tweet

More Decks by Ken’ichiro Oyama

Other Decks in Technology

Transcript

  1. 6 ςεταΠζ෼ྨʹ͓͚Δ net/http/httptest.Server ͷϝϦοτ ςεταΠζ • ςετͷ࣮ߦ଎౓ͱܾఆੑʹண໨ͨ͠ςετ෼ྨ • ॻ੶ʮGoogleͷιϑτ΢ΣΞΤϯδχΞϦϯάʯΑΓ •

    Smallςετʢখςετʣ... ୯ҰϓϩηεʢγϯάϧεϨουʣ಺Ͱ࣮ߦ͞Εͳ͚Ε͹ͳΒͳ ͍ɻsleepېࢭɻI/OېࢭɻϒϩοΫېࢭ • Mediumςετʢதςετʣ... ୯ҰϚγϯ಺Ͱ࣮ߦ͞Εͳ͚Ε͹ͳΒͳ͍ɻlocalhostҎ֎ͷγε ςϜ΁ͷωοτϫʔΫݺͼग़͠ېࢭ • Largeςετʢେςετʣ... ෳ਺Ϛγϯʹ·͕ͨΔςετ
  2. 9 ςεταΠζ෼ྨʹ͓͚Δ net/http/httptest.Server ͷϝϦοτ Takuto Wada. “αόϯφศΓʙࣗಈςετʹؔ͢Δ࿈ࡌͰಘΒΕͨ஌ݟͷ·ͱΊʢ2023೥5݄൛ʣʙ / Automated Test

    Knowledge from Savanna 202305 edition” https://speakerdeck.com/twada/automated-test-knowledge-from-savanna-202305-edition?slide=34
  3. 10 ςεταΠζ෼ྨʹ͓͚Δ net/http/httptest.Server ͷϝϦοτ Takuto Wada. “αόϯφศΓʙࣗಈςετʹؔ͢Δ࿈ࡌͰಘΒΕͨ஌ݟͷ·ͱΊʢ2023೥5݄൛ʣʙ / Automated Test

    Knowledge from Savanna 202305 edition” https://speakerdeck.com/twada/automated-test-knowledge-from-savanna-202305-edition?slide=34 goroutineϕʔεͷMediumςετ
  4. 11 ςεταΠζ෼ྨʹ͓͚Δ net/http/httptest.Server ͷϝϦοτ goroutineϕʔεͷMediumςετͷϝϦοτ • net/http/httptest.Server͸goroutineϕʔεͷMediumςετΛ࣮ݱͰ͖Δ • ҰൠతͳMediumςετͱൺ΂ͯߏ੒ཁૉͷ؅ཧΛGo Runtime

    (Scheduler) ʹ೚ͤΔ͜ͱ͕Ͱ ͖ΔͷͰʢܰྔͰʣ҆ఆͨ͠ӡ༻͕ՄೳʹͳΔ • ʮίεύ͕ྑ͍ʯMediumςετͱݴ͑Δ • ผϓϩηεͰ࣮ݱ͢ΔMediumςετͱൺ΂ͯɺಉҰͷGo Runtime্Ͱ࣮ߦͰ͖ΔͨΊ஋ͷ૬ ޓड͚౉͠ͳͲͷ࿈ܞ͕༰қʹ࣮ݱͰ͖Δ • ςετμϒϧʢϞοΫɺελϒɺεύΠͳͲʣͷ࣮૷͕͠΍͍͢
  5. • ࣗಈςετΛؚΉςετεΠʔτͷ༻ҙ͸ૣ͚Ε͹ૣ͍΄ͲಘΒΕΔޮՌ͕େ͖͍ • ͔͠͠ɺ։ൃॳظ͸ΞϓϦέʔγϣϯͷΞʔΩςΫνϟ͸ະ׬ঢ়ଶ • ΞϓϦέʔγϣϯͷ֎ଆ͔ΒͷςετΛް͘͢Δ͜ͱͰɺΞϓϦέʔγϣϯ಺෦ͷ ΞʔΩςΫνϟมߋʹڧ͘͢Δ → Mediumςετ •

    ֎෦ͱͷΠϯλʔϑΣʔεͰ͋ΔAPIεΩʔϚ΍ɺσʔλϕʔεεΩʔϚɺ֎෦ APIΛத৺ʹςετΛ૊Έཱ͍ͯͯ͘ • goroutineϕʔεͷMediumςετ͸༗ޮͳΞϓϩʔν 13 goroutineϕʔεͷMediumςετΛ޿͘׆༻͢Δςετઓུ ΞϓϦέʔγϣϯͷΞʔΩςΫνϟະ׬ঢ়ଶͰ༗ޮͳςετઓུΛߟ͑Δ
  6. • ࣗಈςετΛؚΉςετεΠʔτͷ༻ҙ͸ૣ͚Ε͹ૣ͍΄ͲಘΒΕΔޮՌ͕େ͖͍ • ͔͠͠ɺ։ൃॳظ͸ΞϓϦέʔγϣϯͷΞʔΩςΫνϟ͸ະ׬ঢ়ଶ • ΞϓϦέʔγϣϯͷ֎ଆ͔ΒͷςετΛް͘͢Δ͜ͱͰɺΞϓϦέʔγϣϯ಺෦ͷ ΞʔΩςΫνϟมߋʹڧ͘͢Δ → Mediumςετ •

    ֎෦ͱͷΠϯλʔϑΣʔεͰ͋ΔAPIεΩʔϚ΍ɺσʔλϕʔεεΩʔϚɺ֎෦ APIΛத৺ʹςετΛ૊Έཱ͍ͯͯ͘ • goroutineϕʔεͷMediumςετ͸༗ޮͳΞϓϩʔν • ίεύ͕ྑ͍ɻ҆ఆ͍ͯ͠ΔɻςετμϒϧΛ࣮ࢪ͠΍͍͢ 16 goroutineϕʔεͷMediumςετΛ޿͘׆༻͢Δςετઓུ ΞϓϦέʔγϣϯͷΞʔΩςΫνϟະ׬ঢ়ଶͰ༗ޮͳςετઓུΛߟ͑Δ
  7. 19 goroutineϕʔεͷMediumςετͷ࡞੒Λαϙʔτ͢Δ github.com/k1LoW/httpstub • ೚ҙͷϨεϙϯεΛฦ͢HTTPαʔόΛ؆୯ʹ૊Έཱͯͯىಈ͢Δ͜ͱ͕Ͱ͖Δ • ͞·͟·ͳ৚݅΍ϨεϙϯεΛઃఆͰ͖ΔͷͰ೚ҙͷHTTPαʔόΛߏஙͰ͖Δ • OpenAPI Spec

    v3ͷDocumentΛಡΈࠐΜͰϦΫΤετͱϨεϙϯεͷόϦσʔγϣϯ͕Ͱ͖Δ • OpenAPI Spec v3ͷDocumentͷ examples: ηΫγϣϯͷ஋Λ࢖ͬͯϨεϙϯεΛฦ͢͜ͱ͕Ͱ ͖Δ
  8. • OpenAPI Spec v3 Documentͷ examples: ηΫγϣϯͷ஋Λ࢖ͬͯϨεϙϯεΛฦ͢ • GET /api/v1/users

    ͸ examples: ηΫγϣϯͷ 2xx ͳεςʔλεͷ஋Λ࢖ͬͯϨεϙϯεΛฦ͢ 21 goroutineϕʔεͷMediumςετͷ࡞੒Λαϙʔτ͢Δ github.com/k1LoW/httpstub
  9. • runn͸1ͭҎ্ͷγφϦΦΛ࣮ߦͯ͠ɺͦΕͧΕͷ੒ޭ/ࣦഊΛΈΔ • 1ͭͷγφϦΦ͸1ͭҎ্ͷεςοϓ͔Βߏ੒͞Ε͍ͯΔ • 1γφϦΦ1YAMLϑΝΠϧʹͳ͍ͬͯΔʢϥϯϒοΫͱݺΜͰ͍Δʣ • γφϦΦ͝ͱʹϥϯφʔͱݺΜͰ͍Δεςοϓ࣮ߦίϯϙʔωϯτΛఆٛ͠ɺͦͷϥϯφʔΛ࢖֤ͬͯε ςοϓΛ࣮ߦ͍ͯ͘͠ •

    ϥϯφʔʹ͸γφϦΦ಺ʹఆ͕ٛඞཁͳ௨ৗͷϥϯφʔͱɺطʹrunnͰ༻ҙ͍ͯ͠ΔϏϧτΠϯϥϯφʔͷ2 छྨ͕͋Δ • ϥϯφʔ: HTTPϥϯφʔɺDBϥϯφʔɺgRPCϥϯφʔɺCDPϥϯφʔɺSSHϥϯφʔ • ϏϧτΠϯϥϯφʔ: ExecϥϯφʔɺTestϥϯφʔɺDumpϥϯφʔɺIncludeϥϯφʔɺBindϥϯφʔ 30 goroutineϕʔεͷMediumςετͷ࡞੒Λαϙʔτ͢Δ runnͷΞʔΩςΫνϟ
  10. • ҰൠతͳMediumςετͱൺ΂Δͱɺಉ͡Goͷίʔυϕʔεʹด͓ͯ͡Γೝ஌ෛՙ͕খ͘͞ײ͡Δ • σʔλϕʔεʹ͍ͭͯ͸ผϓϩηεͳͷͰैདྷ௨Γ • ʮClientΛϞοΫ͢ΔςετʯΑΓ΋ɺຊདྷͷڍಈʹ͍ۙʮServer-ClientͰ࣮ࡍʹ௨৴͢Δςετʯ ͷ΄͏͕௚ײత͔΋ͯ͠ͳ͍ • 1γφϦΦ͋ͨΓͷΧόϨοδ͕େ͖͘ɺ݁ՌϦϑΝΫλϦϯά΁ͷ଱ੑ͕େ͖͘ͳ͍ͬͯΔ •

    γφϦΦͷ֦ॆ΋YAMLΛॻ͚ͩ͘Ͱྑ͍ɻҰ౓૊ΈࠐΜͰ͠·͑͹ɺ͋ͱ͸YAMLΛॻ͚ͩ͘ʹͳ Δ • γφϦΦ͕OpenAPI SpecϥΠΫͰ͋Δ͜ͱ͔ΒΤϯδχΞʹͱͬͯಡΈ΍͘͢ɺͦͷ··ΦϯϘʔ σΟϯάʹ໾ʹཱͬͨʢAPIར༻ଆ΁ͷઆ໌ͳͲʣɻॻ͖΍͍͢=ಡΈ΍͍͢ 34 ࣮ફͯ͠ײ͡ΔϝϦοτͱσϝϦοτ ϝϦοτ